如何在拨打电话后恢复背景音乐(iOS)?

发布于 2024-12-12 06:52:54 字数 609 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素手挽清风 2024-12-19 06:52:54

你的代码奇怪的是,你告诉你确实使用了 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

澉约 2024-12-19 06:52:54

在 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.

国产ˉ祖宗 2024-12-19 06:52:54

你可以恢复它。完成音频会话后,您必须通知其他应用程序我的应用程序已停用音频会话,您可以使用它。使用以下代码。

[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

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.

[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文