最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android利用代碼控制設(shè)備上其他音樂播放器的方法

 更新時間:2018年06月07日 11:45:36   作者:一s獨(dú)秀  
這篇文章主要給大家介紹了關(guān)于Android利用代碼如何控制設(shè)備上其他音樂播放器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

由于最近項(xiàng)目遇到,藍(lán)牙手表設(shè)備在不配對的情況下監(jiān)聽按鍵給出相應(yīng)的控制回應(yīng),所以研究了下

網(wǎng)上找了很多不過對于現(xiàn)在來說,很多手機(jī)設(shè)置沒有反應(yīng),這里給出一個比較統(tǒng)一的方法

項(xiàng)目需求如下圖:

項(xiàng)目需求

方法如下:

*這里主要是為了控制的實(shí)現(xiàn)其他的不多說,直接上代碼,只是記錄下以后也許還會用到

 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);
}

下面這個是控制系統(tǒng)媒體音量鍵的加減

 // 調(diào)低音量
 vAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);

 // 調(diào)高音量
 vAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論

开化县| 南丰县| 卫辉市| 平塘县| 同江市| 彭阳县| 太谷县| 寻甸| 苗栗县| 鄯善县| 沾化县| 右玉县| 陆良县| 沙坪坝区| 大厂| 淳化县| 道孚县| 望谟县| 宁海县| 衡水市| 纳雍县| 潜山县| 家居| 临朐县| 勐海县| 商河县| 泽普县| 阳信县| 大关县| 长兴县| 马关县| 昆明市| 郧西县| 浮梁县| 沁阳市| 瑞昌市| 金坛市| 满洲里市| 栾川县| 杨浦区| 遂昌县|