简体   繁体   English

AVAudioPlayer 帮助:同时播放多个声音,一次停止所有声音,并解决自动引用计数

[英]AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting

I am trying to create buttons that play single sound files and one button that stops all of the sounds that are currently playing.我正在尝试创建play单个声音文件的button和一个stops当前正在播放的所有声音的按钮。 If the user clicks multiple buttons or the same button in a short amount of time, the app should be playing all of the sounds simultaneously.如果用户在短时间内点击多个按钮或同一个button ,应用程序应该同时播放所有声音。 I have accomplished this without much difficulty using the System Sound Services of iOS.我使用 iOS 的系统声音服务毫不费力地完成了这项工作。 However, the System Sound Services play the sounds through the volume that the iPhone's ringer is set to.但是,系统声音服务通过iPhone's铃声设置的volume播放声音。 I am now trying to use the AVAudioPlayer so that users can play the sounds through the media volume.我现在正在尝试使用AVAudioPlayer以便用户可以通过媒体音量play声音。 Here is the code that I am currently (yet unsuccessfully) using the play the sounds:这是我目前(但未成功)使用播放声音的代码:

-(IBAction)playSound:(id)sender
{
   AVAudioPlayer *audioPlayer;
   NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Hello" ofType:@"wav"];
   audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
   [audioPlayer prepareToPlay];
   [audioPlayer play];
}

Whenever I run this code in the iPhone Simulator, it does not play the sound but does display a ton of output.每当我在 iPhone 模拟器中运行此代码时,它不会播放声音但会显示大量输出。 When I run this on my iPhone, the sound simply does not play.当我在我的 iPhone 上运行它时,根本就没有声音。 After doing some research and testing, I found that the audioPlayer variable is being released by Automatic Reference Counting.经过一些研究和测试,我发现audioPlayer变量是由自动引用计数释放的。 Also, this code works when the audioPlayer variable is defined as an instance variable and a property in my interface file, but it does not allow me to play multiple sounds at once.此外,当 audioPlayer 变量在我的接口文件中被定义为一个实例变量和一个属性时,这段代码可以工作,但它不允许我一次播放多个声音。

First thing's first: How can I play an infinite number of sounds at once using the AVAudioPlayer and sticking with Automatic Reference Counting?首先是第一件事:如何使用AVAudioPlayer一次播放无限数量的声音并坚持使用自动引用计数? Also: When these sounds are playing, how can I implement a second IBAction method to stop playing all of them?另外:当这些声音正在播放时,我怎样才能实现第二个IBAction方法来停止播放所有这些声音?

First off, put the declaration and alloc/init of audioplayer on the same line.首先,将audioplayer的声明和分配/初始化放在同一行。 Also, you can only play one sound per AVAudioPlayer BUT you can make as many as you want simultaneously.此外,每个AVAudioPlayer只能播放一种声音,但您可以同时播放任意数量的声音。 And then to stop all of the sounds, maybe use a NSMutableArray , add all of the players to it, and then iterate though and [audioplayer stop];然后停止所有的声音,也许使用NSMutableArray ,将所有播放器添加到它,然后迭代和[audioplayer stop];

//Add this to the top of your file
NSMutableArray *soundsArray;

//Add this to viewDidLoad
soundsArray = [NSMutableArray new]

//Add this to your stop method
for (AVAudioPlayer *a in soundsArray) [a stop];

//Modified playSound method
-(IBAction)playSound:(id)sender {
     NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Hello" ofType:@"wav"];
     AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
     [soundsArray addObject:audioPlayer];
     [audioPlayer prepareToPlay];
     [audioPlayer play];
  }

That should do what you need.那应该做你需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM