现在开始做音乐播放器的模块,遇到了几个问题
当播放音乐的过程中,去调节音量或者情景模式中的铃声设置,结果会有两种声音同时响起。引起此问题的原因是音乐焦点问题没弄清
现分析一下音乐焦点的几个属性:源码在frameworks/base/media/java/andorid/media/AudioManager.java中
public static final int AUDIOFOCUS_NONE = 0;
指示申请得到的Audio Focus不知道会持续多久,一般是长期占有;获得了Audio Focus;
public static final int AUDIOFOCUS_GAIN = 1;
指示要申请的AudioFocus是暂时性的,会很快用完释放的;
public static final int AUDIOFOCUS_GAIN_TRANSIENT = 2;
不但说要申请的AudioFocus是暂时性的,还指示当前正在使用AudioFocus的可以继续播放,只是要“duck”一下(降低音量)。
public static final int AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3;
public static final int AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = 4;
AudioManager.OnAudioFocusChangeListener是申请成功之后监听AudioFocus使用情况的Listener,后续如果有别的程序要竞争AudioFocus,都是通过这个Listener的onAudioFocusChange()方法来通知这个Audio Focus的使用者的。
失去了Audio Focus,并将会持续很长的时间
public static final int AUDIOFOCUS_LOSS = -1 * AUDIOFOCUS_GAIN;
暂时失去Audio Focus,并会很快再次获得。必须停止Audio的播放,但是因为可能会很快再次获得AudioFocus,这里可以不释放Media资源;
public static final int AUDIOFOCUS_LOSS_TRANSIENT = -1 * AUDIOFOCUS_GAIN_TRANSIENT;
暂时失去AudioFocus,但是可以继续播放,不过要在降低音量。
public static final int AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK =
-1 * AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK;
android AudioManager AUDIOFOCUS,布布扣,bubuko.com
android AudioManager AUDIOFOCUS
原文地址:http://blog.csdn.net/tfslovexizi/article/details/37902831