Search code examples
androidandroid-mediaplayermedia

Android: playing a song file using default music player


Is there a way to play media with the default media player? I can do this with the following code:

 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);

But this launches a player with less controls and can't be pushed to the background. Can I launch the player with the default media player?


Solution

  • Try the below code:::

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

    Updated:: Try this also

       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);