AVAudioPlayer帮助:同时演奏多个声音,阻止他们全部一次,和周围自动引用计数工作 [英] AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting

查看:182
本文介绍了AVAudioPlayer帮助:同时演奏多个声音,阻止他们全部一次,和周围自动引用计数工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个单一的播放声音文件,并且停止所有当前播放声音的一键按钮。如果用户点击多个按钮或在很短的时间量相同的按钮,该应用应该是同时玩所有的声音。我已经不使用的iOS系统声音服务太大的困难做到了这一点。但是,系统声音服务通过iPhone的铃声设置为音量播放声音。我现在想使用 AVAudioPlayer ,以便用户可以通过媒体音量播放声音。这里是code,我用播放声音我目前(还没有成功):

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模拟器这个code,它不播放声音但显示一个壮士断腕的。当我在我的iPhone上运行此,声音干脆不玩了。做一些研究和测试后,我发现 audioPlayer 变量正在被自动引用计数释放。此外,当audioPlayer变量被定义为一个实例变量,在我的接口文件的属性此code的作品,但它不会让我玩一次多个声音。

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 的声明,并分配/初始化在同一行。此外,您只能打每 AVAudioPlayer 一个声音,但你可以让你同时想要的。然后停止所有的声音,也许使用的NSMutableArray ,所有的玩家添加到它,然后遍历虽然和 [audioplayer站] ;

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆