我有一个需要播放声音的运动应用程序.我使用AVAudioPlayer播放声音.但是当音频开始播放时,来自另一个应用程序(无线电流应用程序)的背景音乐将被关闭.
如何让它不中断背景音乐?因为我希望用户在锻炼时听到音乐.
谢谢.
Suj*_*han 26
您可以在使用AVAudioPlayer的地方使用以下代码:
// Set AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
Run Code Online (Sandbox Code Playgroud)
如果您想设置委托,可以添加以下代码行:
[[AVAudioSession sharedInstance] setDelegate:self];
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我希望背景音频以较低的音量播放,因为我的应用播放的声音更像是一个警报.我用这个:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// prevents our app from stopping other audio,
// e.g. music, podcats, etc. that may be playing when launched
// our audio will be played at a higher volume
// and the background audio will "duck" until done
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryMultiRoute
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:nil];
}
Run Code Online (Sandbox Code Playgroud)
基于SujithPt的答案,这里的内容与swift相同:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8213 次 |
| 最近记录: |