I’ve just spent about 2 hours trying to get this to work. I’ve never had much luck with getting videos to play with MediaPlayer. What am I doing wrong here? It plays just audio the first time through, no video. And then on the second time I get IllegalStateException‘s when I try to re-setDataSource. I’ve tried not calling stop() and release() in onCompletion I’ve tried calling reset() before playing. I just get different StateExceptions (0, 64, 128) I’ve run out of ideas.
private void playVideo() {
mMediaPlaying = true;
sv.setVisibility(View.VISIBLE); //surfaceview
try {
if(mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDisplay(sh); //surfaceholder
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test));
mp.prepare();
//mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("", "mp prepared");
mp.start();
}
@Override
public void onCompletion(MediaPlayer arg0) {
sv.setVisibility(View.GONE);
mp.stop();
mp.release();
mMediaPlaying = false;
Log.d("", "Done playing media");
}
Ok, I basically copy/pasted this here and it seems to be working… Except the first time it plays there is still no video… But I got rid of the errors 🙂