2

How can i launch a process from with my android app? I am wanting to launch the default music player to play a selected song. I can play the song from within my own app, but i want to let the default player play the song. How would i go about doing this?

Thanks. This is what i got to work using your example.

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
            Uri data = Uri.parse("file://" + pathtofile); 
            intent.setDataAndType(data,"audio/mp3"); 
            try { 
                      startActivity(intent); 
               } catch (ActivityNotFoundException e) { 
                      e.printStackTrace(); 
               }
coolblaze03
  • 51
  • 1
  • 5

2 Answers2

3

Try using ACTION_VIEW:

Uri myResourceToPlay = new Uri(...);
Intent playIntent = new Intent(Intent.ACTION_VIEW, myResourceToPlay);
startActivity(playIntent);
WorkerThread
  • 2,195
  • 2
  • 19
  • 23
2

I usually grab the mime and use that when launching a intent.

    try {
        //launch intent
        Intent i = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory())); 
        String url = uri.toString();

        //grab mime
        String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                MimeTypeMap.getFileExtensionFromUrl(url));

        i.setDataAndType(uri, newMimeType);
        startActivity(i);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Tommie
  • 1,945
  • 2
  • 14
  • 23