我有一个称为LocalAudio的音频对象数组。要播放音频,请使用AVAudioPlayer。我已经实现了委托方法:audioPlayerDidFinishPlaying:,在其中我放置了代码以在LocalAudio数组中切换下一个声音。

 -(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
 {
    [self switchTrack:nextAudioIndex];
 }


音频在后台播放,但声音不会切换。那么如何在后台模式下切换音频呢?
对于调试,我使用iPad 2。

最佳答案

我使用相同的逻辑并且它正在工作,所以其他一些东西必须不正确。

编辑:


-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)播放器成功:(BOOL)标志
{
    如果(标志==否)
        NSLog(@“播放未成功完成”);

    [玩家setCurrentTime:0.0];


    [backgroundMusicPlayer发行];
    backgroundMusicPlayer = nil;


    如果(![音乐isEqualToString:@“ 10”]){
        [self playMusicWithKey:[NSString stringWithFormat:@“%i”,([音乐intValue] + 1)] timesToRepeat:0];
    }
}


哪里

-(void)playMusicWithKey:(NSString *)theMusicKey timesToRepeat:(NSUInteger)theTimesToRepeat {

    NSError *错误;
    NSString * path = [musicLibrary objectForKey:theMusicKey];

    //初始化AVAudioPlayer

    如果(backgroundMusicPlayer == nil){

                backgroundMusicPlayer = [[AVAudioPlayer分配] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
                [backgroundMusicPlayer setDelegate:自我];
                [backgroundMusicPlayer setVolume:backgroundMusicVolume];
                musicLenght = backgroundMusicPlayer.duration;
        }

    //如果backgroundMusicPlayer对象为nil,则出现错误
    if(!backgroundMusicPlayer){
        NSLog(@“ ERROR SoundManager:无法播放键'%@''的音乐,theMusicKey);
        返回;
    }

    //设置此音乐应重复的次数。 -1表示从不停止,直到其被要求停止
    [backgroundMusicPlayer setNumberOfLoops:TheTimesToRepeat];

    //播放音乐
    [backgroundMusicPlayer prepareToPlay];
    [backgroundMusicPlayer播放];

}

10-08 02:31