暂停和继续第三方应用的语音播放
很多语音聊天播放软件都具有在播放聊天语音时暂停外部正在播放的第三方播放器,语音信息播放结束再重新继续第三方播放器,正巧开发中需要这个功能,浏览一下网上资料,搜到的第一手资料是通过sendBroadcast的方法,具体的实现可以参看如下链接
http://stackoverflow.com/questions/14910360/how-to-play-or-resume-music-of-another-music-player-from-my-code
我在做的时候,发现暂停之后如果需要再重新继续播放,那么只能暂停时发“togglepause”命令,发完以后再发一个“togglepause”命令继续。以下是参考代码:
private boolean mOtherPlaying = false;
private void stopOtherPlayer(){
AudioManager vAudioManager = (AudioManager) ShellApp.GetInst().getSystemService(Context.AUDIO_SERVICE);
boolean vIsActive = vAudioManager.isMusicActive();
if (vAudioManager.isMusicActive()) {
mOtherPlaying = true;
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "togglepause");
ShellApp.GetInst().sendBroadcast(i);
}
}
private void ResumeOtherPlayer(){
AudioManager vAudioManager = (AudioManager) ShellApp.GetInst().getSystemService(Context.AUDIO_SERVICE);
boolean vIsActive = vAudioManager.isMusicActive();
if (mOtherPlaying) {
mOtherPlaying = false;
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "togglepause");
ShellApp.GetInst().sendBroadcast(i);
}
}
上面这个方法有个弊端就是,不适用于第三方MP3播放器,只能暂停和继续部分使用原生代码的播放器,所以只是作为参考方案,实用性不高。
后来看到先后看到两篇文章,特别是下面这篇觉得整理的还是不错的
http://lipeng88213.iteye.com/blog/1935470#comments
当然,我自己是先看到了下面这篇文章,再进行调试的(他的代码中还是有点问题的)
http://blog.csdn.net/chenchuntong/article/details/8813719
我在实际使用中发现音频焦点的两个处理函数AudioManager::requestAudioFocus和AudioManager::abandonAudioFocus,还是挺管用的,不管能够暂停MP3,还能暂停视频,用起来还是挺方便的。下面给出一段自己使用的代码,由于我们只在聊天的过程中简短打断和播放下声音,所以假如被外部获得焦点就没有考虑焦点回来时具体的续放功能,失去焦点就直接停止音频消息的播放了。下面是部分代码
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch(focusChange){
case AudioManager.AUDIOFOCUS_GAIN:
Log.i(TAG, "AudioFocusChange AUDIOFOCUS_GAIN");
mAudioFocus = true;
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
Log.i(TAG, "AudioFocusChange AUDIOFOCUS_GAIN_TRANSIENT");
mAudioFocus = true;
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
Log.i(TAG, "AudioFocusChange AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK");
mAudioFocus = true;
break;
case AudioManager.AUDIOFOCUS_LOSS:
Log.i(TAG, "AudioFocusChange AUDIOFOCUS_LOSS");
mAudioFocus = false;
stop();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.i(TAG, "AudioFocusChange AUDIOFOCUS_LOSS_TRANSIENT");
mAudioFocus = false;
stop();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.i(TAG, "AudioFocusChange AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
mAudioFocus = false;
stop();
break;
default :
Log.i(TAG, "AudioFocusChange focus = " + focusChange);
break;
}
}
};
private void requestAudioFocus() {
Log.v(TAG, "requestAudioFocus mAudioFocus = " + mAudioFocus);
if(!mAudioFocus){
int result = mAudioManager.requestAudioFocus(afChangeListener,
AudioManager.STREAM_MUSIC, // Use the music stream.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mAudioFocus = true;
}else{
Log.e(TAG, "AudioManager request Audio Focus result = " + result);
}
}
}
private void abandonAudioFocus() {
Log.v(TAG, "abandonAudioFocus mAudioFocus = " + mAudioFocus);
if (mAudioFocus) {
mAudioManager.abandonAudioFocus(afChangeListener);
mAudioFocus = false;
}
}
暂时小结到这里吧。