Search code examples
androidmedia-player

Calling the default media player with an URI on Android


I found this code:

Uri u = 
            Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, 
            "1"); 
            i.setData(url); 
            startActivity(i); 

That plays a sound with the default media player. I want to call the same media player with a URI that contains a URL.

How can I target the default player?


Solution

  • You need to set mimetype as well.

    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
    mediaIntent.setDataAndType(Uri.parse(url), mimeType);
    startActivity(mediaIntent);