android videoview garbled in new activity
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开发者_StackOverflow社区 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.
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!
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...
精彩评论