Android开发Android技术知识Android进阶之路

Android 代码控制设备上其他音乐播放器

2018-06-06  本文已影响86人  一s独秀

由于最近项目遇到,蓝牙手表设备在不配对的情况下监听按键给出相应的控制回应,所以研究了下
网上找了很多不过对于现在来说,很多手机设置没有反应,这里给出一个比较统一的方法

项目需求

*这里主要是为了控制的实现其他的不多说,直接上代码,只是记录下以后也许还会用到

 private long eventtime = 0;
 private AudioManager vAudioManager = null;    
  //此处在onCreate方法中初始化    
 eventtime = SystemClock.uptimeMillis();
 vAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);


  //这是播放或者暂停
  if (vAudioManager.isMusicActive()){
                Toast.makeText(getApplicationContext(), "有音乐在播放---暂停", Toast.LENGTH_SHORT).show();
                pauseMusic();//暂停
     }else {
                Toast.makeText(getApplicationContext(), "无音乐在播放--开始", Toast.LENGTH_SHORT).show();
                playMusic();//播放
    }

*主要控制代码

  /**
 * 暂停
 */
private void pauseMusic() {
    if (eventtime<=0)return;
    Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
    downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
    sendOrderedBroadcast(downIntent, null);

    Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
    upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
    sendOrderedBroadcast(upIntent, null);
}

/**
 * 播放
 */
private void playMusic() {
    if (eventtime<=0)return;
    Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY, 0);
    downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
    sendOrderedBroadcast(downIntent, null);

    Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY, 0);
    upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
    sendOrderedBroadcast(upIntent, null);
}



/**
 * 上一曲
 */
private void lastMusic() {
    if (eventtime<=0)return;
    Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
    downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
    sendOrderedBroadcast(downIntent, null);

    Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
    upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
    sendOrderedBroadcast(upIntent, null);
}

/**
 * 下一曲
 */
private void nextMusic() {
    if (eventtime<=0)return;
    Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
    downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
    sendOrderedBroadcast(downIntent, null);

    Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
    upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
    sendOrderedBroadcast(upIntent, null);
}
上一篇下一篇

猜你喜欢

热点阅读