AVAudioPlayer无法同时播放声音 [英] AVAudioPlayer not playing sounds simultaneously

查看:73
本文介绍了AVAudioPlayer无法同时播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试同时为AVAudioPlayer播放几个声音文件.我在 AVAudioPlayer帮助中阅读了一个说明如何执行该操作的线程:同时播放多种声音,一次停止所有声音,然后解决自动参考计数问题.当我尝试一次只播放一个文件(self.options.on为ON)时,这似乎是一个奇怪的问题,但是当我尝试创建多个实例(else语句)却不播放它们时,这似乎是一个奇怪的问题.播放.我检查了输出文件URL是否正确,这是什么,所以它不起作用的原因是什么?

I am trying to play a couple of sounds files at the same time for the AVAudioPlayer. I read a thread explaining how to do it @ AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting . It seems to be a weird issue as I when i try to only play one file at a time (when self.options.on is ON), it works, but when I try creating multiple instances (the else statements) and play them nothing plays. I checked to see if the outputfile URL is correct and it is, so what is the reason that it isn't working?

谢谢!

- (void)playTapped:(id)sender 
{
    if (!recorder.recording)
    {
        if(self.options.on)
        {
            AVAudioSession *session = [AVAudioSession sharedInstance];
            [session setCategory:AVAudioSessionCategoryPlayback error:nil];

            NSArray *pathComponents = [NSArray arrayWithObjects:
                                       [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                       [NSString stringWithFormat:@"sound%i.m4a",last],
                                       nil];
            NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

            player = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
            NSLog(@"%@",player.url);
            [player setDelegate:self];
            [player play];
        }
        else
        {
            // trying to play all sounds at once
            for (NSString *str in self.soundsCreated){
                NSLog(@"%@",str);
                NSArray *pathComponents = [NSArray arrayWithObjects:
                                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                           [NSString stringWithFormat:@"sound%@.m4a",str],
                                           nil];
                NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

                AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
                NSLog(@"%@",audioPlayer.url);
                [audioPlayer prepareToPlay];
                [audioPlayer setDelegate:self];
                [audioPlayer play];

            }
        } 
    }
}

注意 我尝试使用NSError检查进行初始化,但未收到任何错误:

NOTE I tried initializing with NSError check but i'm not getting any errors:

NSError *sessionError = nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:&sessionError];
if (sessionError)
{
    NSLog(@"Error in audioPlayer: %@",[sessionError localizedDescription]);
}

解决方案 我的NSMutableArray s初始化似乎是个问题.

SOLUTION It seemed to be an issue with the initialization of my NSMutableArrays.

我从以下位置将它们更改了

I changed them from:

self.soundsCreated = [[NSMutableArray alloc]init];
self.playerArray = [[NSMutableArray alloc]init];

self.soundsCreated = [NSMutableArray new];
self.playerArray = [NSMutableArray new];

并修复了该问题

推荐答案

而不是将错误传递为nil,请尝试并检查初始化是否正确.

Instead of passing error as nil try this and check if initialization is working correct.

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error) 
{
   DebugLog(@"Error in audioPlayer: %@",[error localizedDescription]);
}

这篇关于AVAudioPlayer无法同时播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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