Android利用代碼控制設(shè)備上其他音樂播放器的方法
前言
由于最近項(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ì)腳本之家的支持。
- iOS音樂播放器實(shí)現(xiàn)代碼完整版
- 基于vue-element組件實(shí)現(xiàn)音樂播放器功能
- Android版音樂播放器
- android實(shí)現(xiàn)音樂播放器進(jìn)度條效果
- vue一個(gè)頁(yè)面實(shí)現(xiàn)音樂播放器的示例
- 原生JS實(shí)現(xiàn)網(wǎng)頁(yè)手機(jī)音樂播放器 歌詞同步播放的示例
- python使用Tkinter實(shí)現(xiàn)在線音樂播放器
- python實(shí)現(xiàn)簡(jiǎn)易云音樂播放器
- Android媒體開發(fā)之音樂播放器
- C語(yǔ)言音樂播放器實(shí)例代碼
相關(guān)文章
android常見手動(dòng)和自動(dòng)輪播圖效果
這篇文章主要為大家詳細(xì)介紹了android常見手動(dòng)和自動(dòng)輪播圖效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android實(shí)現(xiàn)帶動(dòng)畫效果的可點(diǎn)擊展開TextView
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶動(dòng)畫效果的可點(diǎn)擊展開TextView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
android實(shí)現(xiàn)上下滾動(dòng)的TextView
android實(shí)現(xiàn)上下滾動(dòng)的TextView,需要的朋友可以參考一下2013-05-05
Android實(shí)現(xiàn)圖片裁剪處理的操作步驟
這篇文章介紹了構(gòu)建具有圖片選擇、裁剪(含手動(dòng)縮放和旋轉(zhuǎn))及保存到自定義路徑功能的 Android 應(yīng)用 demo 的步驟,包括設(shè)置權(quán)限、創(chuàng)建布局文件、實(shí)現(xiàn)自定義視圖CustomCropImageView、更新Activity邏輯等,最終完成了具有完整裁剪功能的應(yīng)用,需要的朋友可以參考下2025-01-01
Android仿微信朋友圈實(shí)現(xiàn)滾動(dòng)條下拉反彈效果
這篇文章主要為大家介紹了Android仿微信朋友圈實(shí)現(xiàn)滾動(dòng)條下拉反彈效果,感興趣的小伙伴們可以參考一下2016-01-01
Android實(shí)現(xiàn)圖片疊加效果的兩種方法
這篇文章主要介紹了Android實(shí)現(xiàn)圖片疊加效果的兩種方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)圖片疊加效果的兩種操作方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-08-08
Android動(dòng)態(tài)表格的實(shí)現(xiàn)代碼(內(nèi)容、樣式可擴(kuò)縮)
這篇文章主要介紹了Android動(dòng)態(tài)表格的實(shí)現(xiàn)代碼(內(nèi)容、樣式可擴(kuò)縮),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

