iPhone:在后台播放音频播放列表?
我正在尝试使用 AVAudioPlayer 在后台播放音频文件序列。当应用程序不在后台时,它播放得很好,但是当应用程序进入后台时,它不会播放第二首歌曲。
这是我的代码:
-(void)playAudio:(NSString *)path{
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
AVAudioPlayer *ap = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
ap.delegate = self;
mainController.audioPlayer = ap;
[ap release];
if (mainController.audioPlayer == nil)
NSLog([error description]);
else{
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
[mainController.audioPlayer prepareToPlay];
if([mainController.audioPlayer play]){
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}
if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid)
[[UIApplication sharedApplication] endBackgroundTask: bgTaskId];
bgTaskId = newTaskId;
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"finished now next...");
[self playAudio:[self getNextAudio]];
}
我调试了应用程序,似乎当它要播放第二首歌曲时 [mainController.audioPlayer play] 返回 NO,这意味着它无法播放。 那么你觉得怎么样?
更新
经过一些测试,似乎只有在设备锁定时它才能继续正常播放,但如果用户按下主页按钮并且应用程序进入后台,问题仍然存在
I'm trying to play sequence of audio files in the background using AVAudioPlayer. When the app is not in the background it plays well, but when the app goes to the background it won't play the second song.
Here is my code:
-(void)playAudio:(NSString *)path{
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
AVAudioPlayer *ap = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
ap.delegate = self;
mainController.audioPlayer = ap;
[ap release];
if (mainController.audioPlayer == nil)
NSLog([error description]);
else{
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
[mainController.audioPlayer prepareToPlay];
if([mainController.audioPlayer play]){
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}
if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid)
[[UIApplication sharedApplication] endBackgroundTask: bgTaskId];
bgTaskId = newTaskId;
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"finished now next...");
[self playAudio:[self getNextAudio]];
}
I debugged the app and it seems that when it's about to play the second song [mainController.audioPlayer play] returns NO, which means it can't play.
So what do you think?
UPDATE
After some testing it seems that it does continue to play properly only if the device locks, but if the user presses the home button and the app goes to the background the problem still remains
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案
只需添加
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
和一些其他调整。一切都在这里https://devforums.apple.com/message/264397
THE SOLUTION
Just add
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
and some other tweaks. It's all herehttps://devforums.apple.com/message/264397