启动时停止背景音频 [英] Stop Background audio while starting

查看:138
本文介绍了启动时停止背景音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过在info.plist文件中添加设置来制作应用程序,以便在移动到背景时播放音频。

I could make the application, to play the audio when it moves to the background, by adding the setting in info.plist file.

但是,当其他一些iOS应用程序播放背景音频,当前应用程序到达前台时,如何停止其他应用程序调用的背景音频?

But, when some other iOS application plays background audio, and when the current application comes to foreground, how to stop the background audio invoked by other application?

推荐答案

查看Apple的AVAudioSession类和AVAudioSession编程指南。
这有望引导您朝着正确的方向前进。

Have a look at AVAudioSession class and AVAudioSession programming guide by Apple. This will hopefully steer you in the right direction.

要在应用程序启动时停止背景音频,请将音频会话设置为不可混合的类别和模式然后设置为活动。

To stop background audio when your application starts set your audio session to a non-mixable category and mode then set active.

如果您没有配置音频会话,您的应用程序将获得默认会话,在您的情况下,该会话不是您所追求的。

If you don't configure an audio session your application will get the default session which in your case is not what you're after.

在你的applicationDelegates方法 - (BOOL)应用程序中尝试这个:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Try this in you applicationDelegates method - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

// set audio session category AVAudioSessionCategoryPlayAndRecord with no options
success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
if (!success) {
    NSLog(@"setCategoryError");
}

// set audio session mode to default
success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
if (!success) {
    NSLog(@"setModeError");
}

// activate audio session
success = [[AVAudioSession sharedInstance] setActive:YES error:nil];
if (!success) {
    NSLog(@"activationError");
}

这篇关于启动时停止背景音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆