android-Managing Audio Playback
2015-12-30 14:00
501 查看
To ensure a great user experience, it’s also important that your app manages the audio focus to ensure multiple apps aren’t playing audio at the same time.
Controlling Your App’s Volume and Playback
Learn how to ensure your users can control the volume of your app using the hardware or software volume controls and where available the play, stop, pause, skip, and previous media playback keys.
Managing Audio Focus
With multiple apps potentially playing audio it's important to think about how they should interact. To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback. Learn how to request the audio focus, listen for a loss
of audio focus, and how to respond when that happens.
Dealing with Audio Output Hardware
Audio can be played from a number of sources. Learn how to find out where the audio is being played and how to handle a headset being disconnected during playback.> If your app plays
media it’s important that your users can control the volume of your app using the hardware or software volume controls of their device, bluetooth headset, or headphones.
To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback—only apps that hold the audio focus should play audio.
> Specifically, the possible focus loss events mirror the focus request types from the previous section—permanent loss, transient
(temporary)loss, and transient with ducking permitted.
> Ducking is the process of lowering your audio stream output volume to make transient audio from another app easier to hear without totally disrupting the audio from your own application.
A loss of audio focus is the most important broadcast to react to, but not the only one. The system broadcasts a number of intents to alert you to changes in user’s audio experience.
》 You can query the
to the device speaker, wired headset, or attached Bluetooth device as shown in the following snippet:
Luckily the system broadcasts an
when this happens. It’s good practice to register a
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.
Controlling Your App’s Volume and Playback
Learn how to ensure your users can control the volume of your app using the hardware or software volume controls and where available the play, stop, pause, skip, and previous media playback keys.
Managing Audio Focus
With multiple apps potentially playing audio it's important to think about how they should interact. To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback. Learn how to request the audio focus, listen for a loss
of audio focus, and how to respond when that happens.
Dealing with Audio Output Hardware
Audio can be played from a number of sources. Learn how to find out where the audio is being played and how to handle a headset being disconnected during playback.> If your app plays
media it’s important that your users can control the volume of your app using the hardware or software volume controls of their device, bluetooth headset, or headphones.
<receiver android:name=".RemoteControlReceiver"> <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver>
public class RemoteControlReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) { // Handle key press. } } } }
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE); ... // Start listening for button presses am.registerMediaButtonEventReceiver(RemoteControlReceiver); ... // Stop listening for button presses am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback—only apps that hold the audio focus should play audio.
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE); ... // Request audio focus for playback int result = am.requestAudioFocus(afChangeListener, // Use the music stream. AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { am.registerMediaButtonEventReceiver(RemoteControlReceiver); // Start playback. }
// Request audio focus for playback int result = am.requestAudioFocus(afChangeListener, // Use the music stream. AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { // Start playback. }
> Specifically, the possible focus loss events mirror the focus request types from the previous section—permanent loss, transient
(temporary)loss, and transient with ducking permitted.
AudioManager.OnAudioFocusChangeListener afChangeListener = new AudioManager.OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT) { // Pause playback } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // Resume playback } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { am.unregisterMediaButtonEventReceiver(RemoteControlReceiver); am.abandonAudioFocus(afChangeListener); // Stop playback } } };
> Ducking is the process of lowering your audio stream output volume to make transient audio from another app easier to hear without totally disrupting the audio from your own application.
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { // Lower the volume } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // Raise it back to normal } } };
A loss of audio focus is the most important broadcast to react to, but not the only one. The system broadcasts a number of intents to alert you to changes in user’s audio experience.
》 You can query the
AudioManagerto 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? }
Luckily the system broadcasts an
ACTION_AUDIO_BECOMING_NOISYintent
when this happens. It’s good practice to register a
BroadcastReceiverthat
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); }
相关文章推荐
- Android:活动的启动模式
- [置顶]android 内存溢出的一些想法
- android自定义控件初解
- android测试工具
- 对于Android stdio 新建工程出错的经验
- Android开发之应用与开发环境(一)
- Android的短信的使用
- Android 使用ORMLite 操作数据库
- android:activity活动的生命周期
- android 十进制颜色值转化为十六进制颜色值的方法
- Android开发笔记(三十四)Excel文件的读写
- Fragment+ViewPager(一)
- android studio 9patch图
- 深入理解:Android 编译系统
- android studio添加第三方.os库
- Silk编解码在android实现
- Android 5.1 Lollipop Phone工作流程浅析(十三)__InCallActivity启动Performance浅析
- speex编解码在android上实现
- ilbc编解码在android实现
- android 状态栏、标题栏、屏幕高度