Android launching music player using intent

I found one way to do this.

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
intent.setDataAndType(Uri.parse(YOUR_SONG_PATH), "audio/*");  
startActivity(intent);

There are number of way by which you can achieve default audio player but those are device and OS specific.

With this code snippet you can get default audio player.

try
    {
    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File("audiofilepath"); 
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(myIntent);
    }
    catch (Exception e) 
    {
         e.printStackTrace();
    }

To simply launch the music player do:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);

Tags:

Android