AVAudioPlayer 帮助:同时播放多个声音,同时停止它们,并解决自动引用计数 [英] AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting
问题描述
我正在尝试创建播放
单个声音文件的按钮和一个按钮
停止
当前正在播放的所有声音的按钮.如果用户在短时间内点击多个按钮或同一个 button
,应用程序应该同时播放所有声音.我使用 iOS 的系统声音服务毫无困难地完成了这项工作.但是,系统声音服务通过 iPhone 的
铃声设置的 volume
播放声音.我现在尝试使用 AVAudioPlayer
以便用户可以通过媒体音量play
声音.这是我目前(但未成功)使用播放声音的代码:
I am trying to create buttons that play
single sound files and one button
that stops
all of the sounds that are currently playing. 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. I have accomplished this without much difficulty using the System Sound Services of iOS. However, the System Sound Services play the sounds through the volume
that the iPhone's
ringer is set to. I am now trying to use the AVAudioPlayer
so that users can play
the sounds through the media volume. 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];
}
每当我在 iPhone 模拟器中运行此代码时,它都不会播放声音,但会显示大量输出.当我在我的 iPhone 上运行它时,声音根本不播放.经过一些研究和测试,我发现 audioPlayer
变量正在被自动引用计数释放.此外,当将 audioPlayer 变量定义为接口文件中的实例变量和属性时,此代码也有效,但它不允许我同时播放多个声音.
Whenever I run this code in the iPhone Simulator, it does not play the sound but does display a ton of output. When I run this on my iPhone, the sound simply does not play. After doing some research and testing, I found that the audioPlayer
variable is being released by Automatic Reference Counting. 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.
第一件事:如何使用 AVAudioPlayer
一次播放无限数量的声音并坚持使用自动引用计数?另外:当这些声音正在播放时,我如何实现第二个 IBAction
方法来停止播放所有这些声音?
First thing's first: How can I play an infinite number of sounds at once using the AVAudioPlayer
and sticking with Automatic Reference Counting? Also: When these sounds are playing, how can I implement a second IBAction
method to stop playing all of them?
推荐答案
首先,将audioplayer
的声明和alloc/init放在同一行.此外,每个 AVAudioPlayer
您只能播放一种声音,但您可以同时制作任意数量的声音.然后停止所有声音,也许使用 NSMutableArray
,将所有播放器添加到它,然后迭代和 [audioplayer stop];
First off, put the declaration and alloc/init of audioplayer
on the same line. Also, you can only play one sound per AVAudioPlayer
BUT you can make as many as you want simultaneously. 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];
//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.
这篇关于AVAudioPlayer 帮助:同时播放多个声音,同时停止它们,并解决自动引用计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!