您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在Android中利用代碼控制設(shè)備上其他音樂播放器,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
項目需求如下圖:
項目需求
方法如下:
*這里主要是為了控制的實現(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);
看完上述內(nèi)容,你們對怎么在Android中利用代碼控制設(shè)備上其他音樂播放器有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。