Search code examples
androidandroid-mediaplayerandroid-video-player

Play video after the buffering was completed in background


I'm trying play a video stream with MediaPlayer in a new Activity after the buffering was completed in background. Why another Activity? It's a part of a video playback library, and the video has to overlay the user's app, no matter what layout is currently active.

My idea is to create a MediaPlayer object, start buffering (with prepare method) and after the buffering is complete start a new Activity with a SurfaceView for showing the video. In the new Activity I assign the SurfaceHolder of the SurfaceView to the MediaPlayer (with setDisplay method) and initiate playback, without success: I see the SurfaceView without the video playback.

I think, the problem is that the MediaPlayer's prepare method expects, that the SurfaceHolder was set with the setDisplay method before it.

Any ideas to get this setup working?


Solution

  • Well, after weeks of trying and futher investigation, here is the answer:

    With the current versions of Android SDK there is no way to preload a video in background with the VideoView or MediaPlayer due to the fact that the latter needs a created Surface to start preloading. Thus, your VideoView or your MediaPlayer has to be (temporary) a visible part of the application's layout before you can start preloading (with prepare() or prepareAsync()). This solution requires injecting of these Views in the user's layout or starting a temporary Activity to get the Surface initialized, which is too invasive for my subject (video SDK).

    If you requirement is to show a video without letting the user wait for a completed buffering and you dont have the control about the application's layout, I would suggest to store the complete video to the sd-card and start the playback afterwards.