如何在拨打电话后恢复背景音乐(iOS)?
我正在使用 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];
}
但这同样不会导致音乐恢复。关于如何解决这个问题有什么想法吗?
I'm using AVAudioPlayer to play music (background supported). My question is if I want to call somebody while I'm listening to the music, how to resume it after the call? I've implement the AVAudioPlayer's delegate method:
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)thePlayer {
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[self.player play];
}
but this will not allow the music to continue.
I've also tried to use the AVAudioSessionDelegate method (just a try):
- (void)viewDidLoad {
[[AVAudioSession sharedInstance] setDelegate:self];
}
- (void)endInterruption
{
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[self.player play];
}
but again this won't cause the music to resume. Any ideas about how to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码奇怪的是,你告诉你确实使用了 AVAudioPlayer,为什么你还要实现 AVAudioSessionDelegate?
而且委托方法的实现很糟糕。请参阅文档
来自 AVAudioPlayerDelegate:
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
所以尝试像这样实现它们,它应该是工作正常
strange from your code is, you're telling that you do use AVAudioPlayer, why do you then implement AVAudioSessionDelegate?
And you have bad implementation of delegate methods. See docs
from AVAudioPlayerDelegate:
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
So try to implement them like this, and it should be working properly
在 iOS 上,应用程序在后台时无法开始播放音频。如果您的应用程序的音频因前台应用程序控制了音频会话而停止,则(当前)无法将其恢复。
On iOS, an app can not start playing audio when already in the background. If your app's audio is stopped because a foreground app has taken control of the audio session, there is (currently) no way to take it back.
你可以恢复它。完成音频会话后,您必须通知其他应用程序我的应用程序已停用音频会话,您可以使用它。使用以下代码。
You can resume it. After finishing your audio session you have to notify other app that my app deactivate the audio session and you can make use of it. Use the following code.