audio - 无法使用AVQueuePlayer在iOS5中的后台播放音频

标签 audio ios5 background avqueueplayer

我正在尝试构建一个用于播放本地音乐的应用程序,但不幸的是,我无法使用AVQueuePlayer在iOS5的后台播放音频。

在我的ViewDidLoad中,我得到了以下代码:

// Player setup
mAudioPlayer = [[AVQueuePlayer alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[mAudioPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0, 1) queue:NULL usingBlock:^(CMTime time) {
    [self updatePositionOnDisplay]; 
}];

// Audio session setup
NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];

这是我的“playerItemDidReachEnd”方法:
- (void)playerItemDidReachEnd:(NSNotification*)notification 
{
    NSLog(@"playerItemDidReachEnd");

    UIBackgroundTaskIdentifier newTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:newTaskID];
    }];
    NSLog(@"playerItemDidReachEnd 2");

    NSLog(@"searching next song");
    mCurrentSong = [self getNextSongWithIsSwitched:NO];
    if(mCurrentSong != nil){
        NSLog(@"Start : %@ - %@", mCurrentSong.artist, mCurrentSong.title);

        mTapeTitle.text = [NSString stringWithFormat:@"%@ - %@", mCurrentSong.artist, mCurrentSong.title];

        AVPlayerItem* i = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:mCurrentSong.path]];

        if(i != nil){
            [mAudioPlayer insertItem:i afterItem:nil];
        }else
            NSLog(@"BING!! no AVPlayerItem created for song's path: %@", mCurrentSong.path);

        [i release];
    }else{
        NSLog(@"no song found");
        [mAudioPlayer pause];
        isPlaying = NO;        
        [mPlayButton setSelected:NO];
    }

    [[UIApplication sharedApplication] endBackgroundTask:newTaskID];
    newTaskID = UIBackgroundTaskInvalid;
}

当我开始播放时,它可以正常工作,并且当我关闭屏幕时可以继续播放。但是当歌曲结束时,这是日志
2012-03-01 10:00:27.342 DEMO[3096:707] playerItemDidReachEnd
2012-03-01 10:00:27.360 DEMO[3096:707] playerItemDidReachEnd 2
2012-03-01 10:00:27.363 DEMO[3096:707] searching next song
2012-03-01 10:00:27.381 DEMO[3096:707] Start : Moby - Ah-Ah

但是没有一首歌有效地开始...

谁能告诉我我的代码有什么问题吗?

非常感谢。

最佳答案

尝试注释下一行

[[UIApplication sharedApplication] endBackgroundTask:newTaskID];
newTaskID = UIBackgroundTaskInvalid;

如果工作正常,那么当状态为AVPlayerStatusReadyToPlay时,您需要为“currentItem.status”将一个观察者添加到mAudioPlayer中,然后结束后台任务

关于audio - 无法使用AVQueuePlayer在iOS5中的后台播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9513494/

相关文章:

button - 通过两个按钮在两个剪辑之间切换音频

css - 如何禁止在浏览器中查看图像?

matlab - 在Matlab中从声音文件读取数据

python - PyAudio在Linux上播放缓慢

javascript - 如何在 windows7(WebRTC,C++)的 chrome(CEF3)中设置音频输出(扬声器)

ios - 单击 uitableviewcell 时使用 Actionsheet 而不是 segue

ios - 奇怪的游戏中心行为?

iphone - 手动同步 iCloud

html - CSS3 背景 : repeating-linear-gradient

css - Android、Joomla 中的背景显示不正确