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:
<
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());
}