Media Player setDataSource

Asked

Viewed 465 times

6

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

I used:

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

But it doesn’t work.

3 answers

2

The method setDataSource() has several overloads, the one you are trying to use gets a path.
The method toString() class Uri does not return a path but rather the representation, in string, 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);

You’re not actually using Uri who built on the line:

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

You should therefore use the Overload of the method setDataSource() who receives a Uri:

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

0

Works if it is a local file, but it is a streaming file (a url)

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

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

  • You can explain better what you mean. I can’t understand your answer.

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"

That file plays a video if it’s on a server.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.