1

我正在实现音频会话中断的 C 监听器。当它被要求中断时,我会停用我的音频会话。然后当我的应用程序恢复时,我会再次激活音频会话。我为我的音频会话设置了许多属性和类别,我是否必须在重新激活后重置所有内容?

提前致谢。

一些代码供参考:

初始化、设置类别:

OSStatus error = AudioSessionInitialize(NULL, NULL, interuptListenerCallBack, (__bridge void *)(self));
UInt32 category = kAudioSessionCategory_PlayAndRecord;
error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
if (error) printf("couldn't set audio category!");
//use speaker as default
UInt32 doChangeDefaultOutput = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultOutput), &doChangeDefaultOutput);
//allow bluethoothInput 
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, sizeof(allowBluetoothInput), &allowBluetoothInput);

interuptListenerCallBack 是我因中断而停用和响应音频会话的地方,使用

 OSStatus error = AudioSessionSetActive(false);
 if (error) printf("couldn't deactivate audio session!");

或者

  OSStatus error = AudioSessionSetActive(true);
  if (error) printf("AudioSessionSetActive (true) failed");
4

1 回答 1

3

如果您正确使用了音频会话中断侦听器,那么不,您不必重置属性。您只需要确保您实际调用kAudioSessionBeginInterruptionand kAudioSessionEndInterruption。我不确定你的听众是什么样的,但如果你正在做这样的事情:

if (inInterruptionState == kAudioSessionBeginInterruption) {
        AudioSessionSetActive(NO);
    }
    if (inInterruptionState == kAudioSessionEndInterruption) {

        AudioSessionSetActive(YES);
    }

并且遵循音频会话的规则,那么理论上,你不应该重置你的属性。

我不知道您使用音频会话的目的是什么,但您也可以使用以下命令暂停和恢复播放:

kAudioSessionInterruptionType_ShouldResume 

kAudioSessionInterruptionType_ShouldNotResume.

您可以按照文档中的说明使用这些:

kAudioSessionInterruptionType_ShouldResume

表示刚刚结束的中断是适合立即恢复播放的中断;例如,来电被用户拒绝。

在 iOS 4.0 及更高版本中可用。

在 AudioSession.h 中声明。

kAudioSessionInterruptionType_ShouldNotResume

表示刚刚结束的中断是不适合恢复播放的中断;例如,您的应用程序被 iPod 播放中断。

在 iOS 4.0 及更高版本中可用。

在 AudioSession.h 中声明。

您应该阅读文档,因为那里有很多关于暂停、恢复和处理 AudioSession 中断的信息。

笔记:

自 iOS7 以来,AudioSession 已被弃用。请改用AVAudioSession方法,或通过设置常量AVAudioSessionInterruptionOptions或来设置 Pause 和 Resume 选项AVAudioSessionInterruptionType

(自 iOS 6 起可用)

于 2013-09-05T21:15:35.170 回答