Android:使用默认音乐播放器播放歌曲文件

rah*_*hul 2 media android android-mediaplayer

有没有办法使用默认媒体播放器播放媒体.我可以使用以下代码

 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);
Run Code Online (Sandbox Code Playgroud)

但是这会启动一个控制较少的玩家,并且无法将其推向后台.我可以使用默认媒体播放器启动播放器吗?

谢谢!拉胡尔.

Sha*_*wal 7

试试以下代码:::

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

更新::也尝试这个

   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);
Run Code Online (Sandbox Code Playgroud)

  • 我在使用第二个选项时遇到Activitynotfoundexception.我不确定为什么会这样! (2认同)
  • 我收到了这个日志:无法找到明确的活动类{com.android.music/com.android.music.MediaPlaybackActivity}; 你有没有在AndroidManifest.xml中声明这个活动? (2认同)