0

I'm loading an mp4 video from external storage into a videoview in every activity. In the first activity, the video is perfect, in the second activity, the video is garbled, and then in the third activity, the video is perfect again. Do I need to somehow clean up the videoview before loading a new activity? The video is consistently garbled following a clear video, and consistently clear after a garbled video.

I tried setting the onpreparedlistener and then starting the mediaplayer in the onprepared method, but every other video still remains garbled. However, when I break at the onprepared method, the video from the previous activity is still visible and running only in the cases where the new video is garbled. When the new activity blacks out the old video, the new video is not garbled.

It seems like the video is persisting from the previous activity to the new activity every other time. I'm confused.

Jays
  • 145
  • 3
  • 8

2 Answers2

2

I had a similar issue before - what is happening is that your video is not completely buffered or in a READY state before you begin playing it. Take a look at the state diagram for the MediaPlayer here.

This is pretty much what you need to do.

It is persisting into the next activity because the media player keeps playing its contents until it is done playing them. Do this:

@Override
public void onDestroy() { 
    super.onDestroy();
    if (mediaPlayerInstance.isPlaying()) 
        mediaPlayerInstance.stop();
}

That should solve your problem!

Community
  • 1
  • 1
digerati32
  • 623
  • 2
  • 6
  • 17
  • Thanks, I added the listener but it seems like that's not the problem. I edited the question. – Jays Sep 23 '11 at 20:57
  • I had the same issue as well :D! In onDestroy() call your mediaPlayer instance and do: `if (mediaPlayerInstance.isPlaying()) mediaPlayer.Instance.stop();` That will stop it from playing or continuing on to the next activity. – digerati32 Sep 24 '11 at 00:14
  • I actually need to call reset. stop() just pauses the video so it's still being displayed in the next activity, which causes the next video to become garbled. Also, this shouldn't be done in onDestroy since it's called by the system only after you call finish() or when it's clearing memory. Even if you call finish() before starting the new activity, the system decides when to call ondestroy, so the new video may try to load before the previous activity's ondestroy is actually called. – Jays Sep 25 '11 at 02:15
  • I previously looked at the videoview.java source code online, and what I find especially weird is that, based on the code I read online, I shouldn't be having this problem. The code I read called reset on the old mediaplayer and then set it to null before opening a new video. – Jays Sep 25 '11 at 02:21
0

Alright I've got it working. I needed to reset the mediaPlayer associated with the videoview before starting the new activity. I guess this means that my new activity is using the same videoview and therefore the same mediaplayer in the new activity.

Although calling reset works, it's hard to believe that that's what I'm supposed to do. It seems like videoview was designed so I don't have to manage the mediaplayer since the only way to access the mediaplayer is through the onPreparedListener...

Jays
  • 145
  • 3
  • 8