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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android 系統(tǒng)實(shí)現(xiàn)多種開機(jī)動(dòng)畫和logo切換功能
這篇文章主要介紹了android 系統(tǒng)實(shí)現(xiàn)多種開機(jī)動(dòng)畫和logo切換功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12android教程之把自己的應(yīng)用加入到系統(tǒng)分享中
在Android系統(tǒng)中打開相冊(cè)中的某張圖片, 點(diǎn)擊右上角的分享按鈕會(huì)彈出分享列表, 把自己的應(yīng)用加入到里面來,下面是設(shè)置方法2014-02-02Android如何實(shí)現(xiàn)時(shí)間線效果(下)
上一篇文章我們講了Android如何實(shí)現(xiàn)時(shí)間線效果,今天計(jì)息上一回的文章圍繞Android實(shí)現(xiàn)時(shí)間線效果內(nèi)容展開更多,需要的朋友可以參考一下2021-11-11詳解Flutter WebView與JS互相調(diào)用簡易指南
這篇文章主要介紹了詳解Flutter WebView與JS互相調(diào)用簡易指南,分為JS調(diào)用Flutter和Flutter調(diào)用JS,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04Android基于繪制緩沖實(shí)現(xiàn)煙花效果
這篇文章主要介紹了Android基于繪制緩沖實(shí)現(xiàn)煙花效果,文中通過代碼示例和圖文結(jié)合介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以自己動(dòng)手嘗試一下2024-03-03Android使用kotlin實(shí)現(xiàn)多行文本上下滾動(dòng)播放
這篇文章主要為大家詳細(xì)介紹了Android使用kotlin實(shí)現(xiàn)多行文本的上下滾動(dòng)播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Android編程實(shí)現(xiàn)圖片的瀏覽、縮放、拖動(dòng)和自動(dòng)居中效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片的瀏覽、縮放、拖動(dòng)和自動(dòng)居中效果,以具體實(shí)例形式分析了Android針對(duì)圖片各種常見顯示效果的布局及功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-11-11Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)案例詳解
這篇文章主要介紹了Android TextWatcher內(nèi)容監(jiān)聽死循環(huán)案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android提高之Android手機(jī)與BLE終端通信
這篇文章主要介紹了Android手機(jī)與BLE終端通信的方法,有很大的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08