I\'ve been really banging my head against the table trying to get the MediaPlayer class to try to play h.264-encoded videos on Android 2.1. My code is rather simple:
<I know this may be too late. I managed to use video from resources as this:
MediaPlayer mp = ...;
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/" + R.raw.my_movie);
mp.setDataSourceUri(this, uri);
If you are using multiple instances of MediaPlayer
, make sure that you've executed release()
on a resource before attempting to use it in a different instance.
first thanks for your code, the open raw ressource helped me with my problem.
I'm sorry I don't have a definite answer to your problem but based on the api media example in the SDK and my media player in my project I would suggest checking that the file opens correctly.
otherwise maybe you need to set the display before preparing as such:
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
those are the only ideas I have
hope it helps
Jason
To playback a video or audio file located in the raw folder use:
mMediaPlayer = MediaPlayer.create(this, R.raw.a_video_or_audio_file);
mMediaPlayer.start();
no need to call prepare() when using MediaPlayer.create(Context,R.raw.some_resource_file)
initilize the fileName, mediaPlayer instance:
private MediaPlayer mp;
private final static String fileName = "ring";
for playing/starting audio file from res/raw directory:
try
{
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_RING); //set streaming according to ur needs
mp.setDataSource(Context, Uri.parse("android.resource://yourAppPackageName/raw/"+fileName));
mp.setLooping(true);
mp.prepare();
mp.start();
} catch (Exception e)
{
System.out.println("Unable to TunePlay: startRingtone(): "+e.toString());
}
finally stopping the audioFile:
try
{
if (!(mp == null) && mp.isPlaying())
{
mp.stop();
mp.release(); //its a very good practice
}
}
catch (Exception e)
{
System.out.println("Unable to TunePlay: stopRingtone():"+e.toString());
}
I found Best and clear solution for this error : java.io.IOException: Prepare failed.: status=0x1
Note
if you want read from your sd card and got this error , your are not set
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
in your Manifast .
Note 2
if your are want play from url and got that error maybe your are set setDataSource
to like this :https://www.android.com
just remove S from http
like this http://www.android.com