Media Player setDataSource

6

The function setDataSource() works with a url , but I need to fetch a file in my raw (for example).

I used:

Uri url = Uri.parse("android.resource://" + ctx.getPackageName() + "/" + R.raw.file);
mediaPlayer.setDataSource(url.toString());

But it does not work.

    
asked by anonymous 11.08.2015 / 12:21

3 answers

2

The setDataSource() method has several overloads , the one you are trying to use receives a path . The method toString() of the Uri class does not return a path but rather the string representation of uri with which it was built.

What you're doing is the equivalent of this:

mediaPlayer.setDataSource("android.resource://" + ctx.getPackageName() + "/" + R.raw.file);

In the background you are not using Uri that you built on the line:

Uri url = Uri.parse("android.resource://" + ctx.getPackageName() + "/" + R.raw.file);

You should therefore use the overload method of setDataSource() that receives a uri :

Uri url = Uri.parse("android.resource://" + ctx.getPackageName() + "/" + R.raw.file);
mediaPlayer.setDataSource(ctx, url);
    
11.08.2015 / 12:40
0

It works if it is a local file, but it is a streaming file (an url)

AssetFileDescriptor afd = ctx.getAssets().openFd("file.txt");
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

where the file.txt has the url for example, does not work.

    
11.08.2015 / 16:06
0

I have a file - file.m3u8 with the following content:

EXTM3U  
    EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="bipbop_audio",LANGUAGE="eng",NAME="BipBop Audio 1",AUTOSELECT=YES,DEFAULT=YES
    EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="bipbop_audio",LANGUAGE="eng",NAME="BipBop Audio 2",AUTOSELECT=NO,DEFAULT=NO,URI="alternate_audio_aac_sinewave/prog_index.m3u8"

This file plays a video if it is on a server.

I intend to have this file inside my apk and play the file using Media Player with setDataSource("file.m3u8")

    
11.08.2015 / 17:54