2012-06-03 94 views
2

有沒有辦法使用默認媒體播放器播放媒體。我可以用下面的代碼做Android:使用默認音樂播放器播放歌曲文件

Intent intent = new Intent(Intent.ACTION_VIEW); 
MimeTypeMap mime = MimeTypeMap.getSingleton(); 
String type = mime.getMimeTypeFromExtension("mp3"); 
intent.setDataAndType(Uri.fromFile(new File(songPath.toString())), type); 
startActivity(intent); 

但是這會啓動一個控制器較少的播放器,不能將其推到背景。 我可以用默認媒體播放器啓動播放器嗎?

Thx! 拉胡爾。

回答

7

試試下面的代碼:::

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER); 
    File file = new File(songPath.toString()); 
    intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
    startActivity(intent); 

更新::試試這也

Intent intent = new Intent(); 
    ComponentName comp = new ComponentName("com.android.music", "com.android.music.MediaPlaybackActivity"); 
    intent.setComponent(comp); 
    intent.setAction(android.content.Intent.ACTION_VIEW); 
    File file = new File(songPath.toString()); 
    intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
    startActivity(intent); 
+0

thx!但是它的功能和我的代碼一樣。我想啓動默認媒體播放器。 – rahul

+0

它啓動默認播放器爲我的想法..也更新我的帖子檢查它 –

+0

第二個選項工作。我們需要在意圖中設置組件對象。謝謝! – rahul

2

我一直在研究這個在過去的幾天,因爲我沒有股票音樂播放器。看起來很悲慘,不能輕易完成。在瀏覽各種音樂應用程序的AndroidManifest.xml以查找線索後,我偶然發現了MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH。

使用下面的方法,只要歌曲位於Android MediaStore中,我就可以在後臺啓動Samsung音樂播放器。您可以指定藝術家,專輯或標題。這種方法也適用於谷歌播放音樂,但遺憾的是,即使普通的Android播放器的最新版本沒有此意圖:

https://github.com/android/platform_packages_apps_music/blob/master/AndroidManifest.xml

private boolean playSong(String search){ 
    try { 
     Intent intent = new Intent(); 
     intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.putExtra(SearchManager.QUERY, search); 
     startActivity(intent); 
     return true; 
    } catch (Exception ex){ 
     ex.printStackTrace(); 
     // Try other methods here 
     return false; 
    } 
} 

這將是很好地發現,使用內容URI的解決方案或URL,但該解決方案適用於我的應用程序。

+0

這實際上適用於note2。涼。謝謝 – zproxy