0

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?

artkoenig
  • 7,117
  • 2
  • 40
  • 61

3 Answers3

1

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.

artkoenig
  • 7,117
  • 2
  • 40
  • 61
0

Start a Async task and inside the body of method doinBackground() start a onBuffering listener ,,, and you can store the data in a cache ,, and then can play the video. may be this can perform your required task.

Pranav Sharma
  • 692
  • 2
  • 9
  • 22
  • The problem is the playback, not with the buffering. The MediaPlayer starts async buffering by calling `prepareAsync` method. I use the `OnPreparedListener` to check if the buffering was complete. – artkoenig Aug 17 '12 at 12:19
0

what i did i tell you and it worked for me ,

made a class extends async and implements OnPrepared Listener, then in method

doinbackgroung(){
Display display =getWindowManager().getDefaultDisplay();
video_view.setOnPreparedListener(this);
video_view.setVideoURI(Uri.parse(path)); 
return null;
} 

and afterwards in onPrepared() just video_view.start();

You can also implement onVideoSizeChangedListener if you want the video both in landscape an dportrait mode to play good.. this will work it seems

Pranav Sharma
  • 692
  • 2
  • 9
  • 22