Reputation: 7257
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?
Upvotes: 0
Views: 1818
Reputation: 7257
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.
Upvotes: 1
Reputation: 684
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
Upvotes: 0
Reputation: 684
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.
Upvotes: 0