用户在播放音乐的时候有多个选择,可以使用内置的扬声器,有线耳机或者是支持A2DP的蓝牙耳机。(补充:A2DP全名是Advanced Audio Distribution Profile 蓝牙音频传输模型协定, A2DP是能够采用耳机内的芯片来堆栈数据,达到声音的高清晰度。有A2DP的耳机就是蓝牙立体声耳机。声音能达到44.1kHz,一般的耳机只能达到8kHz。如果手机支持蓝牙,只要装载A2DP协议,就能使用A2DP耳机了。还有消费者看到技术参数提到蓝牙V1.0 V1.1 V1.2 V2.0——这些是指蓝牙的技术版本,是指通过蓝牙传输的速度,他们是否支持A2DP具体要看蓝牙产品制造商是否使用这个技术。)
检测目前正在使用的硬件设备
选择不同的播放设备会影响应用程序的行为。
可以使用AudioManager来查询某个音频是输出到扬声器、有线耳机还是蓝牙上。代码如下:
if (am.isBluetoothA2dpOn()) {
// Adjust output for Bluetooth.
} else if (am.isSpeakerphoneOn()) {
// Adjust output for Speakerphone.
} else if (am.isWiredHeadsetOn()) {
// Adjust output for headsets
} else {
// If audio plays and noone can hear it, is it still playing?
}
处理音频输出设备的改变
当有线耳机被拔出或者蓝牙设备断开连接的时候,音频流会自动输出到内置的扬声器上。假设之前播放声音很大,这个时候突然转到扬声器播放会显得非常嘈杂。
幸运的是,系统会在这种事件发生时发出广播带有ACTION_AUDIO_BECOMING_NOISY的intent。无论何时播放音频都应该去注册这个BroadcastReceiver来监听这个intent会是比较好的做法。在音乐播放器下,用户通常希望发生那样事情的时候能够暂停当前歌曲的播放。而在游戏里,通常会选择减低音量。
private class NoisyAudioStreamReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
// Pause the playback
}
}
}
private IntentFilter intentFilter =
new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
private void startPlayback() {
registerReceiver(myNoisyAudioStreamReceiver(), intentFilter);
}
private void stopPlayback() {
unregisterReceiver(myNoisyAudioStreamReceiver);
}
视频讲解:http://www.eyeandroid.com/thread-15915-1-1.html
分享到:
相关推荐
Dealing with Audio Ground Loops
基于松弛变量的数据包络分析模型,SBM模型的原理英文版。学习方法的时候找到的。
八年级英语Dealing with troublePPT课件.pptx
There are chapters dealing with all the important aspects of the Android platform, including GUI design, file- and data-handling, coping with phone calls, multimedia apps, interaction with location ...
纹理图像处理领域的著作,Wiley出版社出版.
R scripts for dealing with mturk
八年级英语Dealing with troublePPT学习教案.pptx
Chapter 3 Dealing with trouble测试题2.doc
ASTM E178 - 21 Standard Practice for Dealing With Outlying Obser
- - 与人交往中注意的一些容易被跳过的细节.以及基础理论分析与人交往出现的情况.
- Fixed : Issue with dealing with FTP downloads & caching Update : TAdvTaskDialog v1.9.2.0 - New : FooterTextColor property added to control footer text color for non native dialogs Update : ...
Dealing with Different Hardware and Software Availability Optimizing UI Performance with Strict Mode Chapter 19: Monetizing, Promoting, and Distributing Applications Signing and Publishing ...
teaching you how to deal with all kinds of hard people.
英语作文
【Dealing with Data, User Defaults, SQLite, Web Services】[PDF] [iPhone/iPad/iOS]
Dealing with Different Hardware Writing and Using Parcelables Responding to URLs Plugin Patterns PackageManager Tricks Remote Services and the Binding Pattern Advanced Manifest Tips Miscellaneous ...
Dealing with Different Hardware Writing and Using Parcelables Responding to URLs App Shortcuts PackageManager Tricks Remote Services and the Binding Pattern Advanced Manifest Tips Miscellaneous ...
Multi-Label classification: Dealing with Imbalance by Combining Labels
处理不均衡数据_(深度学习)!_Dealing_with_imbalanced_data_(deep_learning)