I am facing the issue that whenever a stream is played by my app on Android 4.0+ the OnPrepare
method from MediaPlayer.OnPreparedListener
is called even before a stream is loaded and thus i am unable to indicated the user that the stream downloading/buffering is in process. I have already found a question of the same kind but not answered
Here is a what i am doing.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playVideo(someRtspUrl);
}
private void playVideo(String url) {
// if app is running on Google TV then change RTSP link to HLS
if (url.contains("rtsp")) {
// split the RTSP URL and make it as HLS
String videoUrlParts[] = url.split("\\?");
url = videoUrlParts[0].replace("rtsp", "http") + "/playlist.m3u8";
if (videoUrlParts.length > 1)
url += "?" + videoUrlParts[1];
} mVideoView.setVideoURI(Uri.parse(url));
mVideoView.requestFocus();
mVideoView.setOnCompletionListener(this);
mVideoView.setOnPreparedListener(this);
}
@Override
public void onPrepared(MediaPlayer player) {
dismissProgressDialog();
mVideoView.start();
}
This Code is Working fine on Google TV and other Android 3.0+ and < 4.0+ device
The Media Player requires a SurfaceHolder object for displaying video content, assigned using the setDisplay() method. The Surface View is a wrapper around the Surface Holder object. Note that we must implement the SurfaceHoler. Callback interface.
android.media.MediaPlayer. MediaPlayer class can be used to control playback of audio/video files and streams. MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.
Just use pause to stop, followed by seekTo(0) before restarting: mediaPlayer. seekTo(0); mediaPlayer.
Android is providing MediaPlayer class to access built-in mediaplayer services like playing audio,video e.t.c. In order to use MediaPlayer, we have to call a static Method create() of this class. This method returns an instance of MediaPlayer class. Its syntax is as follows − MediaPlayer mediaPlayer = MediaPlayer.
I don't have much experience with media player. But couple of suggestions/queries from my side
I would suggest using the MediaPlayer class that is available. Unlike the VideoView MediaPlayer is aware of its state and manages even more things for you that VideoView doesn't offer.. Paired with setting up a SurfaceHolder to display the content in it is pretty simple.
You'll just want to make sure that you are properly handling the state and using the prepareAsync() call of MediaPlayer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With