博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dealing with Audio Output Hardware 处理音频输出硬件设备
阅读量:4046 次
发布时间:2019-05-24

本文共 1943 字,大约阅读时间需要 6 分钟。

Users have a number of alternatives when it comes to enjoying the audio from their Android devices. Most devices have a built-in speaker, headphone jacks for wired headsets, and many also feature Bluetooth connectivity and support for A2DP audio.

Check What Hardware is Being Used

How your app behaves might be affected by which hardware its output is being routed to.

You can query the to determine if the audio is currently being routed to the device speaker, wired headset, or attached Bluetooth device as shown in the following snippet:

if (isBluetoothA2dpOn()) {    // Adjust output for Bluetooth.} else if (isSpeakerphoneOn()) {    // Adjust output for Speakerphone.} else if (isWiredHeadsetOn()) {    // Adjust output for headsets} else {     // If audio plays and noone can hear it, is it still playing?}//http://blog.csdn.net/sergeycao

Handle Changes in the Audio Output Hardware

When a headset is unplugged, or a Bluetooth device disconnected, the audio stream automatically reroutes to the built in speaker. If you listen to your music at as high a volume as I do, that can be a noisy surprise.

Luckily the system broadcasts an intent when this happens. It’s good practice to register a that listens for this intent whenever you’re playing audio. In the case of music players, users typically expect the playback to be paused—while for games you may choose to significantly lower the volume.

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);}
你可能感兴趣的文章
REST简介
查看>>
理解RESTful架构
查看>>
nginx日志切割
查看>>
js函数的作用域与this指向
查看>>
腾讯QQ团队开源分布式后台服务引擎msec
查看>>
看看腾讯和百度等这样的大型网站系统架构是如何演化的
查看>>
AMQP与QPID简介
查看>>
nginx虚拟主机
查看>>
Nginx 性能调优
查看>>
nginx rewrite规则之last和break
查看>>
Redis和Memcached的区别
查看>>
Memcached 集群的高可用(HA)架构
查看>>
浏览器端的缓存规则
查看>>
redis持久化RDB和AOF
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>
memcached工作原理与优化建议
查看>>
Redis与Memcached的区别
查看>>
redis sharding方案
查看>>
程序员最核心的竞争力是什么?
查看>>
阿里、网易、滴滴共十次前端面试碰到的问题
查看>>