xcode 使用 AVPlayer 播放多个音频文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6193844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:10:06  来源:igfitidea点击:

Play multiple Audio Files with AVPlayer

xcodeipadavfoundationavplayer

提问by Rouslan Karimov

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.

是通过使它们成为 AVAssets,但在这种情况下,我将如何随时停止播放它们。

Really appreciate your help.

真的很感谢你的帮助。

The reason I need AVPlayer is to fetch sounds from the iPod Library.

我需要 AVPlayer 的原因是从 iPod 库中获取声音。

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.

我终于从 Apple Dev Team 的技术支持那里得到了答案,当我决定使用几个 AVPlayer 时,我似乎走在正确的轨道上。

采纳答案by Sam Baumgarten

For every sound you want to make make a new AVPlayer.

对于您想要制作的每个声音,都可以创建一个新的 AVPlayer。

NSURL *url = [NSURL URLWithString:pathToYourFile];
AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];
[audioPlayer play];

NSURL *url = [NSURL URLWithString:pathToYourFile];
AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];
[audioPlayer play];

回答by DotSlash

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..

我从来没有在这里回答过一个问题,我不知道有人还在等待这个问题的答案,但这是我的看法......试试这个,它应该可以工作,我目前正在使用它来播放 12 个以上的同步音频样本。如果我在做一些新手,我深表歉意。

You press a button and you run this code...

你按下一个按钮,然后运行这段代码......

But first you need to:

但首先你需要:

  1. Need to import AVFoundation to project and #import into .h file as well then we can play sound with this.
  2. Need to put "AVAudioPlayer *myAudio;" without quotation marks of course somewhere on top (usually on top of viewDidLoad).
  1. 需要将 AVFoundation 导入到项目中,并将 #import 导入到 .h 文件中,然后我们可以用它播放声音。
  2. 需要把“AVAudioPlayer *myAudio;” 没有引号当然在顶部的某个地方(通常在viewDidLoad 的顶部)。

Then just...

那么就...

-(IBAction)playButtonPressed:(id)sender {

    NSURL *yourMusicFile;
    yourMusicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"your_Song_Name" ofType:@"mp3"]];

    myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [myAudio play];
    NSLog(@"Button -playButtonPressed- has been pressed!");
}

回答by Ravi

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 APIat 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.

好吧,我的解决方案来自经验。如果需要,我可以快速制定一个项目。而且,它还需要在斯坦福使用MoMu API。它涉及创建用于读取文件的 WvIn 和 WvOut 对象。只需将这些对象的音频样本送入输出缓冲区即可同时播放文件。虽然 API 使用了 AVFoundation,但在这个项目中并没有明确使用 AVFoundation。