1

有什麼方法可以在不同品牌的設備上開始使用默認音樂播放器播放歌曲嗎?開始com.android.music.musicservicecommand

我試了下面的代碼,並且如果服務已經運行就暫停工作。然而,播放命令永遠不會啓動。

例如,我在Xperia S.上測試這個Pause工程,但是從未啓動默認的(Walkman應用)播放器(服務)。

final AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

Button player = (Button) findViewById(R.id.player); 
player.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Log.d("Button music:","clicked!"); 

     if(mAudioManager.isMusicActive()) { 
      Log.v("Button music:","paused!"); 
      Intent i = new Intent("com.android.music.musicservicecommand"); 
      i.putExtra("command", "pause"); 
      MainActivity.this.sendBroadcast(i); 
     }else{ 
      Log.v("Button music:","starts!"); 
      Intent i = new Intent("com.android.music.musicservicecommand"); 
      i.putExtra("command", "play"); 
      MainActivity.this.sendBroadcast(i); 
     } 

    } 
}); 

回答

0

試試這個:

Intent intent = new Intent("android.intent.action.MUSIC_PLAYER"); 
    //"songsList" is an array with paths of all the songs in the sdcard 
Uri uri = Uri.parse(songsList.get(0)); 
String type = "audio/mp3"; 
intent.setDataAndType(uri, type); 
startActivity(intent); 

這將直接打開你的音樂播放器&開始選定/特定的歌曲。如果它不工作,那麼你可以通過 「android.intent.category.APP_MUSIC」 取代意圖名

+0

它顯示了一個錯誤:機器人。 content.ActivityNotFoundException:未發現處理Intent的活動{act = android.intent.category.APP_MUSIC dat = file:/// storage/emulated/0/Download/Kannil Kannil.mp3 typ = audio/mp3} –

0

試試這個:

long eventtime = SystemClock.uptimeMillis(); 
    Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 
    KeyEvent downEvent = new KeyEvent(eventtime, eventtime, 
    KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_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_PLAY_PAUSE, 0); 
    upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); 
    sendOrderedBroadcast(upIntent, null); 

鏈接:Android default media player "start" after "stop" error (com.android.music.musicservicecommand)