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

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

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

前言

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

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

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

項(xiàng)目需求

方法如下:

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

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

下面這個(gè)是控制系統(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é)

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

相關(guān)文章

最新評(píng)論

柯坪县| 霍邱县| 东山县| 芷江| 勃利县| 全南县| 剑阁县| 乐至县| 吉林市| 内黄县| 永春县| 儋州市| 新宁县| 宜兴市| 安乡县| 安远县| 徐汇区| 通河县| 临朐县| 突泉县| 依安县| 翼城县| 东丽区| 贵州省| 绍兴县| 旅游| 武清区| 毕节市| 柘城县| 宜都市| 万宁市| 隆化县| 阳原县| 社旗县| 磐石市| 广州市| 久治县| 监利县| 拜城县| 新巴尔虎左旗| 富裕县|