简体   繁体   English

我的应用启动后,其他应用的音乐停止播放

[英]Music from other apps stops playing when my app starts

When I start my app music from other apps stops playing. 当我启动应用程序时,来自其他应用程序的音乐将停止播放。

I've tried adding the setCategory:AVAudioSessionCategoryAmbient in didFinishLaunchingWithOptions but the music still stops on FIRST launch. 我试过在didFinishLaunchingWithOptions添加setCategory:AVAudioSessionCategoryAmbient ,但音乐在setCategory:AVAudioSessionCategoryAmbient启动时仍会停止。 If I start the music and goes back to the app with home button the music will continue to play. 如果我开始播放音乐并使用主屏幕按钮返回到应用程序,则音乐将继续播放。 Is there any workaround for the first launch issue? 有没有针对第一次启动问题的解决方法?

Snippet I've added: 我添加的代码段:

// make sure music plays while sounds play.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

Try this instead: 尝试以下方法:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:0 error:nil];

Or this if above doesn't work: 或如果上面的方法不起作用:

[[AVAudioSession sharedInstance] setActive:NO error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

EDIT: 编辑:

If none of the above works for you, you should maybe try set another category you see fit together with AVAudioSessionCategoryOptionMixWithOthers if that works for your use-case. 如果以上方法都不适合您,那么您可以尝试将您认为合适的其他类别与AVAudioSessionCategoryOptionMixWithOthers一起设置(如果适用于您的用例)。

Determines whether audio from this session is mixed with audio from active sessions in other audio apps. 确定此会话中的音频是否与其他音频应用程序中活动会话中的音频混合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM