使用 AVPlayer 播放多个音频文件
我正在尝试同时播放多种声音。
我最初采取的方法是创建多个玩家,但这似乎是错误的。
同时播放多个音频文件的最佳方式是什么?
是通过将它们制作为 AVAssets 来实现的吗?但是在这种情况下,我如何随时停下来播放它们。
非常感谢您的帮助。
我需要 AVPlayer 的原因是从 iPod 库中获取声音。
我终于从Apple Dev Team的TechSupport得到了答复,当我决定使用多个AVPlayer时,看来我走上了正确的道路。
I'm trying to play multiple sounds at the same time.
The approach initially I've taken was to create several players , but it seems wrong one.
What's the best way to play several audio files at the same time.
Is it through making them AVAssets, but in this case how would I stop and play them whenever I want.
Really appreciate your help.
The reason I need AVPlayer is to fetch sounds from the iPod Library.
I finally got an answer from TechSupport of Apple Dev Team and it seems I'm on the right track when I decided to use several AVPlayer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于您想要制作的每种声音,制作一个新的 AVPlayer。
NSURL *url = [NSURL URLWithString:pathToYourFile];
AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];
[audioPlayer 播放];
For every sound you want to make make a new AVPlayer.
NSURL *url = [NSURL URLWithString:pathToYourFile];
AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];
[audioPlayer play];
我从来没有在这里回答过问题,我不知道还有谁在等待这个问题的答案,但这是我的看法...尝试一下,它应该可以工作,我目前正在使用它来播放 12 个以上的同步音频样本。如果我正在做一些新的事情,我深表歉意。
您按下一个按钮,然后运行此代码...
但首先您需要:
那么就...
I have never answered a question here and I don't know in anyone is still waiting for an answer to this but heres my take... Try this and it should work, I am currently using it to play 12 plus simultaneous audio samples. I apologize if I am doing something newbish..
You press a button and you run this code...
But first you need to:
Then just...
嗯,我的解决方案来自经验。如果需要的话,我可以快速制定一个项目。而且,它还需要使用斯坦福大学的 MoMu API。它涉及创建 WvIn 和 WvOut 对象来读取文件。这些对象的音频样本只需输入到输出缓冲区即可同时播放文件。虽然API使用了AVFoundation,但是在这个项目中并没有明确使用AVFoundation。
Well, my solution comes out of experience. I can quickly cook up a project if needed. But also, it requires the use of an MoMu API at Stanford. It involves creating WvIn and WvOut objects for reading the files. The audio samples of these objects simply need to be fed to the output buffer to play the files simultaneously. Although the API uses AVFoundation, there is no explicit use of AVFoundation in this project.
基本上就像其他人所说的那样,确保为每个源创建一个音频播放器。
您还必须做的是对所有玩家对象保持强有力的引用。
如果您不这样做,它们就会被释放并停止播放。
当我只保留对我想播放的最后一个源的引用时,我遇到了这个问题。这意味着我只会听到最后一个数据源,并且我认为问题与同时播放配置有关 - 但实际上,这只是其他播放器被释放,因此播放会停止。
在创建和播放音频源之前,我还使用了上面的代码片段。
Basically what everyone else is saying, make sure you create an audio player for each source.
What you also must do is KEEP A STRONG REFERENCE TO ALL THE PLAYER OBJECTS.
If you don't do this they get released and playback stops.
I had this issue when I was only keeping a reference to the last source I wanted to play back. This meant that I would only hear the last data source and I thought the issue was something to do with the simultaneous playback configuration - but in reality it was just the other players were being dealloc'd and thus playback would stop.
I also used the above snippet before creating and playing back my audio sources.
我们可以使用带有多个音频资源的AVMutableComposition来制作mixComposition,然后在AVPlayer上播放。就像:
We can make mixComposition with AVMutableComposition with multiple audio asset, then play on AVPlayer. like as: