I'm working on a Podcast app. This plays audio using the AVAudioSessionCategoryPlayback
session type. This does almost everything that I want it to do. It pauses other playing music, it gets interrupted when a call comes in and generally works exactly as I want.
我正在开发一个播客应用程序。它使用AVAudioSessionCategoryPlayback会话类型来播放音频。这几乎做了我想做的所有事情。它会暂停其他音乐的播放,当有电话打进来时,它会被打断,通常情况下,它会按照我的意愿工作。
With one exception. When higher priority audio gets played (for example, when running a turn-by-turn navigation app) the audio in my application gets ducked down in volume but continues to run. This creates an awful blending of the two voices that isn't desired. I would instead prefer for the audio in my application to pause during the over-played sound then continue again once it finishes.
只有一个例外。当播放高优先级的音频时(例如,当运行逐转导航应用程序时),我的应用程序中的音频会被音量调低,但会继续运行。这就产生了两种不希望的声音的可怕混合。相反,我更希望我的应用程序中的音频在播放过度的声音期间暂停,然后在它结束时再次继续。
I've tried changing the different AVAudioSession properties such as AVAudioSessionCategoryOptionMixWithOthers
, but these only change how lower priority sounds are blended with my application. They do not change how mine is blended with higher priority sounds. I also tried observing the otherAudioPlaying
property of the sharedSession
but this didn't fire changes when I these short clips get overlayed.
我尝试过改变不同的AVAudioSession属性,比如avaudiosessioncategoryoptionmixwith其他的,但是这些只会改变低优先级的声音如何与我的应用混合。它们不会改变我的声音如何与更高优先级的声音混合。我也尝试观察sharedSession的otheraudioplay属性,但是当我将这些短剪辑重叠时,这并没有引起更改。
Is there any way to detect when the audio in my app is being ducked so that I could instead have it paused? Or alternatively to prevent the audio of my app from being ducked so that it would treat these other sounds as interruptions?
有什么方法可以检测到我的应用中的音频何时被隐藏,以便我可以让它暂停?或者是防止我的应用程序的声音被闪避,这样它就会把这些声音当作干扰来处理?
Thanks.
谢谢。
6 个解决方案
#1
1
In you AppDelegate.m
Class and in didFinishLaunchingWithOptions
function, listen the following notification
在你AppDelegate。在didFinishLaunchingWithOptions函数中,监听以下通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionInterruption:) name:AVAudioSessionInterruptionNotification object:nil];
and outside the application launching method, implement the selector as
在应用程序启动方法之外,实现选择器as
- (void)audioSessionInterruption:(NSNotification *)notif{
NSLog(@"\n\nInterruption Notification :%@\n\n",notif.userInfo);
}
Hope this will work for you....
希望这将为你工作....
#2
1
I wrote to Apple DTS about this issue. Here is what I got back:
我写信给苹果的DTS关于这个问题。以下是我得到的反馈:
Thank you for contacting Apple Developer Technical Support (DTS). Our engineers have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations.
感谢您联系苹果开发者技术支持(DTS)。我们的工程师已经审查了您的请求,并得出结论,鉴于目前的运输系统配置,没有支持的方法来实现所需的功能。
If you would like for Apple to consider adding support in the future to allow/notify the background app to stop playing audio when the foreground app chooses to duck other audio, then enable the background app to start audio again after ducking completed, please submit an enhancement request via the Bug Reporter tool at http://bugreport.apple.com.
苹果考虑如果你想添加在未来的支持允许/通知后台应用前景时停止播放音频应用选择鸭其他音频,然后启用后台应用程序音频闪避完成后再开始,请提交一个增强请求通过错误记者在http://bugreport.apple.com网站上的工具。
Seems that it's impossible right now unfortunately.
不幸的是,现在看来这是不可能的。
#3
1
This feature was added in iOS 9 and can be enabled by setting your audio session mode to AVAudioSessionModeSpokenAudio. Other apps that speak such as navigation apps will interrupt your audio instead of ducking.
该特性在ios9中添加,可以通过将音频会话模式设置为AVAudioSessionModeSpokenAudio来启用。其他会说话的应用,比如导航应用,会打断你的声音,而不是躲起来。
See: https://developer.apple.com/documentation/avfoundation/avaudiosessionmodespokenaudio
参见:https://developer.apple.com/documentation/avfoundation/avaudiosessionmodespokenaudio
#4
0
I never did this, but I imagine you could use AudioSession services C function audioSessionAddPropertyListener to register your callback for kAudioSessionProperty_OtherAudioIsPlaying.
我从来没有这样做过,但是我认为您可以使用AudioSession services C函数audioSessionAddPropertyListener来为kaudiosessionproperty_otheraudioisplation注册回调。
Be aware there's some confusion in "FLATDACTED" documentation and Apple DevForums about the possibility of all or some AudioSession C functions being deprecated "in the future".
请注意,在“flatdact”文档和Apple dev论坛上存在一些混淆,关于所有或部分AudioSession C函数的可能性在“未来”中被否定。
#5
0
You might want to look into interruptions, and simply discard your audio session when interrupted, then resume it when the interruption ends, as follows:
您可能想要查看中断,并在中断时简单地丢弃您的音频会话,然后在中断结束时恢复它,如下所示:
In your ViewDidLoad:
在你ViewDidLoad:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
if ([self canBecomeFirstResponder]) {
[self becomeFirstResponder];
}
Handle the notifications:
处理的通知:
static NSInteger audioSessionActiveCounter = 0;
- (void)audioSessionDidChangeInterruptionType:(NSNotification *)notification
{
AVAudioSessionInterruptionType interruptionType = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (AVAudioSessionInterruptionTypeBegan == interruptionType)
{
DDLogVerbose(@"Session interrupted: --- Begin Interruption ---");
// stop your session here with a setActive:NO
}
else if (AVAudioSessionInterruptionTypeEnded == interruptionType)
{
DDLogVerbose(@"Session interrupted: --- End Interruption ---");
// resume your session here with a setActive:YES
}
}
Hope it helps.
希望它可以帮助。
#6
0
I don't believe that kAudioSessionProperty_OtherAudioIsPlaying is supposed to call back in any way - it may be that you have to poll this value on a regular basis.
我不认为kaudiosessionproperty_otheraudioisplation应该以任何方式回调——您可能需要定期轮询这个值。
It should return a non-zero value when other audio is being played on the device. This may not indicate that you are being "ducked" per se but it would indicate that other audio is mixing with your audio. Supposedly you would only be ducked if the other app set the kAudioSessionProperty_OtherMixableAudioShouldDuck property of their Audio Session.
当设备上播放其他音频时,它应该返回一个非零值。这可能并不意味着你被“回避”了,但它将表明其他音频正在与你的音频混合。假设只有当另一个应用设置了他们音频会话的kAudioSessionProperty_OtherMixableAudioShouldDuck属性时,你才会被回避。
Regarding polling - yes, it may not be as elegant as a callback, but sometimes it is necessary and I suspect polling this value is a minor hit.
关于轮询——是的,它可能没有回调那么优雅,但有时它是必要的,我怀疑轮询这个值是一个小问题。
Hope this helps.
希望这个有帮助。
#1
1
In you AppDelegate.m
Class and in didFinishLaunchingWithOptions
function, listen the following notification
在你AppDelegate。在didFinishLaunchingWithOptions函数中,监听以下通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionInterruption:) name:AVAudioSessionInterruptionNotification object:nil];
and outside the application launching method, implement the selector as
在应用程序启动方法之外,实现选择器as
- (void)audioSessionInterruption:(NSNotification *)notif{
NSLog(@"\n\nInterruption Notification :%@\n\n",notif.userInfo);
}
Hope this will work for you....
希望这将为你工作....
#2
1
I wrote to Apple DTS about this issue. Here is what I got back:
我写信给苹果的DTS关于这个问题。以下是我得到的反馈:
Thank you for contacting Apple Developer Technical Support (DTS). Our engineers have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations.
感谢您联系苹果开发者技术支持(DTS)。我们的工程师已经审查了您的请求,并得出结论,鉴于目前的运输系统配置,没有支持的方法来实现所需的功能。
If you would like for Apple to consider adding support in the future to allow/notify the background app to stop playing audio when the foreground app chooses to duck other audio, then enable the background app to start audio again after ducking completed, please submit an enhancement request via the Bug Reporter tool at http://bugreport.apple.com.
苹果考虑如果你想添加在未来的支持允许/通知后台应用前景时停止播放音频应用选择鸭其他音频,然后启用后台应用程序音频闪避完成后再开始,请提交一个增强请求通过错误记者在http://bugreport.apple.com网站上的工具。
Seems that it's impossible right now unfortunately.
不幸的是,现在看来这是不可能的。
#3
1
This feature was added in iOS 9 and can be enabled by setting your audio session mode to AVAudioSessionModeSpokenAudio. Other apps that speak such as navigation apps will interrupt your audio instead of ducking.
该特性在ios9中添加,可以通过将音频会话模式设置为AVAudioSessionModeSpokenAudio来启用。其他会说话的应用,比如导航应用,会打断你的声音,而不是躲起来。
See: https://developer.apple.com/documentation/avfoundation/avaudiosessionmodespokenaudio
参见:https://developer.apple.com/documentation/avfoundation/avaudiosessionmodespokenaudio
#4
0
I never did this, but I imagine you could use AudioSession services C function audioSessionAddPropertyListener to register your callback for kAudioSessionProperty_OtherAudioIsPlaying.
我从来没有这样做过,但是我认为您可以使用AudioSession services C函数audioSessionAddPropertyListener来为kaudiosessionproperty_otheraudioisplation注册回调。
Be aware there's some confusion in "FLATDACTED" documentation and Apple DevForums about the possibility of all or some AudioSession C functions being deprecated "in the future".
请注意,在“flatdact”文档和Apple dev论坛上存在一些混淆,关于所有或部分AudioSession C函数的可能性在“未来”中被否定。
#5
0
You might want to look into interruptions, and simply discard your audio session when interrupted, then resume it when the interruption ends, as follows:
您可能想要查看中断,并在中断时简单地丢弃您的音频会话,然后在中断结束时恢复它,如下所示:
In your ViewDidLoad:
在你ViewDidLoad:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
if ([self canBecomeFirstResponder]) {
[self becomeFirstResponder];
}
Handle the notifications:
处理的通知:
static NSInteger audioSessionActiveCounter = 0;
- (void)audioSessionDidChangeInterruptionType:(NSNotification *)notification
{
AVAudioSessionInterruptionType interruptionType = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (AVAudioSessionInterruptionTypeBegan == interruptionType)
{
DDLogVerbose(@"Session interrupted: --- Begin Interruption ---");
// stop your session here with a setActive:NO
}
else if (AVAudioSessionInterruptionTypeEnded == interruptionType)
{
DDLogVerbose(@"Session interrupted: --- End Interruption ---");
// resume your session here with a setActive:YES
}
}
Hope it helps.
希望它可以帮助。
#6
0
I don't believe that kAudioSessionProperty_OtherAudioIsPlaying is supposed to call back in any way - it may be that you have to poll this value on a regular basis.
我不认为kaudiosessionproperty_otheraudioisplation应该以任何方式回调——您可能需要定期轮询这个值。
It should return a non-zero value when other audio is being played on the device. This may not indicate that you are being "ducked" per se but it would indicate that other audio is mixing with your audio. Supposedly you would only be ducked if the other app set the kAudioSessionProperty_OtherMixableAudioShouldDuck property of their Audio Session.
当设备上播放其他音频时,它应该返回一个非零值。这可能并不意味着你被“回避”了,但它将表明其他音频正在与你的音频混合。假设只有当另一个应用设置了他们音频会话的kAudioSessionProperty_OtherMixableAudioShouldDuck属性时,你才会被回避。
Regarding polling - yes, it may not be as elegant as a callback, but sometimes it is necessary and I suspect polling this value is a minor hit.
关于轮询——是的,它可能没有回调那么优雅,但有时它是必要的,我怀疑轮询这个值是一个小问题。
Hope this helps.
希望这个有帮助。