Android录制或播放语音消息时关闭其他媒体播放

1.发送系统广播:
Intent i = new Intent(“com.android.music.musicservicecommand”);
i.putExtra(“command”, “pause”);
context.sendBroadcast(i);
因为其他的播放器不一定注册监听了那个广播,所以大多数情况是不起作用的。

2.通过触发媒体的按钮来操作播放器状态:
KeyEvent.KEYCODE_MEDIA_PAUSE
public static void sendMediaButton(Context context, int keyCode) {
AudioManager audioManager= (AudioManager) context.getSystemService (Context.AUDIO_SERVICE);
//先判断后台是否再播放音乐
if (audioManager.isMusicActive()){
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent,null);

    keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
    intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
    context.sendOrderedBroadcast(intent,null);
}

}

3. 通过AudioManager获取音频焦点AudioFocus,来竞争使用audio做停止播放或者降低声音的处理
AudioManager am = (AudioManager) mAppContext.getSystemService (Context.AUDIO_SERVICE);
am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager .AUDIOFOCUS_GAIN_TRANSIENT);
AudioManager.AUDIOFOCUS_GAIN: 永久获取媒体焦点(播放音乐) 估计音乐播放器使用了此方法,在播放视频时调用,不起作用
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT 暂时获取焦点 适用于短暂的音频
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK Duck 我们应用跟其他应用共用焦点 我们播放的时候其他音频会降低音量

后台播放音乐时,项目中播放视频,1方法不起作用,2方法有效 3 暂时获取焦点时,播放视频,音乐停止,退出播放界面,音乐自动播放

4.写一个服务,当第三方播放器打开时 ,开启这个服务,音乐暂停;关闭服务,音乐继续
服务类:
private AudioManager mAm;
private static boolean vIsActive=false;
private MyOnAudioFocusChangeListener mListener;
public class MyOnAudioFocusChangeListener implements OnAudioFocusChangeListener {
@Override
public void onAudioFocusChange(int focusChange) {
// TODO Auto-generated method stub
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d(TAG, “onStartCommand”);
Toast.makeText(getApplicationContext(), “服务已启动”, 0).show();

            mAm = (AudioManager) getApplicationContext().getSystemService(  
                    Context.AUDIO_SERVICE);  
            vIsActive=mAm.isMusicActive();  
            mListener = new MyOnAudioFocusChangeListener();  
            if(vIsActive)  
            {  
                int result = mAm.requestAudioFocus(mListener,  
                        AudioManager.STREAM_MUSIC,  
                        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);  

                if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)    
                {    
                    Log.d(TAG, "requestAudioFocus successfully.");                                    
                }    
                else    
                {    
                    Log.d(TAG, "requestAudioFocus failed.");    
                }    
            }                 
            return super.onStartCommand(intent, flags, startId);  
        }  

    @Override  
    public void onDestroy() {  
        // TODO Auto-generated method stub  
        super.onDestroy();  
        if(vIsActive)  
        {  
            mAm.abandonAudioFocus(mListener);  
        }  
        Log.d(TAG, "onDestroy");  
    }  

调用:开始或结束服务
startService(new Intent(MainActivity.this,StartService.class));
stopService(new Intent(MainActivity.this,StartService.class));

你可能感兴趣的:(android,android)