我正在使用AVAudioPlayer播放音乐(支持后台)。我的问题是,如果我想在听音乐的时候打电话给某人,如何在通话后恢复通话?我已经实现了AVAudioPlayer的委托方法:
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)thePlayer {
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[self.player play];
}但这将不允许音乐继续播放。
我还尝试使用AVAudioSessionDelegate方法(试一试):
- (void)viewDidLoad {
[[AVAudioSession sharedInstance] setDelegate:self];
}
- (void)endInterruption
{
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[self.player play];
}但这也不会导致音乐继续播放。对如何解决这个问题有什么想法吗?
发布于 2011-10-27 12:52:34
从你的代码中奇怪的是,你告诉我你确实使用了AVAudioPlayer,那么为什么还要实现AVAudioSessionDelegate呢?
而且你的委托方法的实现也很糟糕。请参阅文档
from AVAudioPlayerDelegate:
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
因此,尝试像这样实现它们,它应该可以正常工作
发布于 2011-10-30 03:32:46
在iOS上,应用程序在后台时不能开始播放音频。如果你的应用程序的音频因为前台应用程序已经控制了音频会话而停止,那么(目前)没有办法恢复它。
发布于 2014-01-29 15:39:20
你可以重新开始。使用以下代码。
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];https://stackoverflow.com/questions/7911758
复制相似问题