【问题标题】:AVAudioPlayer not playing sounds simultaneouslyAVAudioPlayer 不同时播放声音
【发布时间】:2013-11-19 05:40:41
【问题描述】:

我正在尝试同时为 AVAudioPlayer 播放几个声音文件。我读了一个线程来解释如何做到这一点@AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting。这似乎是一个奇怪的问题,因为当我尝试一次只播放一个文件时(当 self.options.on 为 ON 时),它可以工作,但是当我尝试创建多个实例(else 语句)并且什么都不播放时戏剧。我检查了输出文件的 URL 是否正确,那么它不起作用的原因是什么?

谢谢!

- (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 check 进行初始化,但没有收到任何错误:

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

解决方案 我的NSMutableArrays 的初始化似乎有问题。

我将它们更改为:

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

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

然后解决了它

【问题讨论】:

    标签: ios iphone objective-c avaudioplayer


    【解决方案1】:

    我在播放小文件时也遇到过同样的情况:

    这是我的解决方案(示例)

    - (void)dropSoundWhileRefresh
    {
        NSError *sessionError = nil;
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
        [[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
        [[AVAudioSession sharedInstance] setDelegate:self];
        NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"waterDrops" ofType:@"mp3"]];
        dropSound = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
        [dropSound setDelegate:self];
        [dropSound setVolume:0.10];
        [dropSound setNumberOfLoops:0];
        [dropSound play];
    }
    

    【讨论】:

    • 我认为这与我的问题@anton 没有任何关系
    • @RoyH,你到底是什么意思?我知道您的问题是,当您尝试播放 2 个音频文件时,第二个会破坏第一个的播放,不是吗?
    【解决方案2】:

    不要将错误传递为 nil 试试这个并检查初始化是否正确。

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

    【讨论】:

    • 感谢@gladiator2345 - 我刚刚试过没有收到任何错误
    • 另外,奇怪的是,当我在数组中只有一个对象时它甚至不起作用......所以我什至不确定它是否与多个文件有关。
    • 尝试创建一个 NSMutableArray 作为实例变量,并在播放前添加玩家。
    • [NSString stringWithFormat:@"sound%i.m4a",last], nil]; [NSString stringWithFormat:@"sound%@.m4a",str], nil]; "sound%i" 和 sound%@ 这可能是问题。验证网址
    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 2014-08-15
    相关资源
    最近更新 更多