如何使用 AVAudioPlayer 的单个实例来播放多首歌曲?

发布于 2024-08-27 23:15:19 字数 103 浏览 4 评论 0原文

我想用 AVAudioplayer 的单个对象播放多首歌曲,我将歌曲放在表行中,当用户点击行播放器视图打开时,但当用户返回表播放器中的其他行时,会同时播放两首歌曲。我可以做什么来解决这个问题?

I want to play multiple songs with single object of AVAudioplayer, I put songs in table row , when user tap on row player view is open but when user go back in other row in table player play both songs simanteniosly . what I Can do to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2024-09-03 23:15:19

我假设您已在 h 文件中创建了该对象,并声明并合成了它的属性。
此外,您可能正在通过 didSelectRowAtIndexPath: 方法播放文件。在该方法中,您可能有以下代码部分。

AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];

[appSoundPlayer prepareToPlay];之前添加[appsoundPlayer stop];只是为了确保音频播放器在开始播放会话之前处于停止状态。

如果您采用不同的方法,只需在此处发布代码的相关部分,

I assume that you have created the object in the h file, and have property declared and synthesized it.
Also you might be playing the file from didSelectRowAtIndexPath: method. In that method you might have the following section of code.

AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];

Before [appSoundPlayer prepareToPlay]; add [appsoundPlayer stop]; just to ensure that the audioplayer is in stop state before starting the playing session.

If you are following a different method, just post the relevant part of the code here,

Oo萌小芽oO 2024-09-03 23:15:19

AVAudioPlayer 用于播放单个音频剪辑。如果您想播放不同的剪辑,请停止当前播放器,使用新的音频数据制作一个新的剪辑,然后播放。

如果两首歌曲重叠,那么您可能不小心创建了两个 AVAudioPlayer 实例,并且未能停止第一个实例。

An AVAudioPlayer is meant to play a single audio clip. If you want to play a different clip, stop the current player, make a new one with the new audio data, and play it.

If two songs are overlapping then you're probably accidentally making two AVAudioPlayer instances and failing to stop the first one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文