900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 声音莫名从扬声器切换到听筒_扬声器听筒的切换

声音莫名从扬声器切换到听筒_扬声器听筒的切换

时间:2019-08-07 13:42:36

相关推荐

声音莫名从扬声器切换到听筒_扬声器听筒的切换

下午研究了一下扬声器听筒切换

如果需要保持插拔耳机之前的状态可以用我总结的代码:如果插耳机之前是扬声器,拔了之后还保存扬声器状态。

if (判断条件) {

// 扬声器

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

} else {

// 听筒

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

}

如果不需要:(enable是判断条件)

[[AVAudioSession sharedInstance] overrideOutputAudioPort:enable ? AVAudioSessionPortOverrideSpeaker:AVAudioSessionPortOverrideNone error:nil];

原文:/vieri_ch/article/details/43733375

本文部分翻译了官方对这两个属性的描述。链接如下,此外加入个人的使用体会

/library/ios/qa/qa1754/_index.html

问题的起源,在iOS中,播放声音时,输出到扬声器的需求。两个属性设置的区别

The difference is that setting the AVAudioSessionPortOverride by calling overrideOutputAudioPort: is more transient than using the category option AVAudioSessionCategoryOptionDefaultToSpeaker.

通过overrideOutputAudioPort这个方法设置,属性AVAudioSessionPortOverride, 比使用AVAudioSessionCategoryOptionDefaultToSpeaker这个属性更短暂

e.g

方法1

[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];

方法2 ,

[audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecordwithOptions:AVAudioSessionCategoryOptionDefaultToSpeakererror:&error];

方法1,设置之后,如果此时插入耳机,在拔掉。播放的声音会从听筒输出,而不是回到扬声器

方法2. 设置之后,始终输出到扬声器,而不是其他接收器,如果没有耳机。(简要的说,就是如果有个蓝牙音箱,哪怕接上都不会有声音输出到蓝牙音响,插上耳机,则会有声音输出到耳机。)

这里有个很重要的地方。这两个属性都只被用于。AVAudioSessionCategoryPlayAndRecord

使用场景的补充

如果在某个场景下,希望强制从speaker输出声音,最好使用

AVAudioSessionCategoryPlayback, 而不是AVAudioSessionCategoryPlayAndRecord,因为后者默认从听筒输出。无外接设备的情况。

[sessionsetCategory:AVAudioSessionCategoryPlaybackerror:&error];

需要使用扬声器和听筒切换场景,用

AVAudioSessionCategoryPlayAndRecord

使用听筒之后,PlayandRecord下切换到扬声器

[audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecordwithOptions:AVAudioSessionCategoryOptionDefaultToSpeakererror:&error];

这篇也有一定参考价值:

/xdrt81y/article/details/38926663

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。