I am confused by the MediaController and VideoView APIs in Android.
I am using it to stream videos through HTTP, which is working fine. Only problem is that it stops for buffering from time to time. I now want to get notified whenever the video starts to buffer to display a progress indicator.
I learned that I need to implement public abstract void onBufferingUpdate (MediaPlayer mp, int percent) from android.media.MediaPlayer.OnBufferingUpdateListener, my question is just where do I have to implement it? I followed the advice from another SO question and implemented:
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(Constants.LOG, "onPrepared called");
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
//do something
Log.d(Constants.LOG, "Buffering update: " + percent + "%");
}
});
}
But it didn't work... Do I have to subclass VideoView and implement it there??
Works:
VideoView myVideoView = (VideoView)this.findViewById(R.id.video_view);
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
myVideoView.start();
if ( dialog != null )
dialog.dismiss();
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener()
{
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
Log.d("URL", "Buffering update: " + percent + "%");
}
});
}
});
Dialog dialog = new ProgressDialog(VideoActivity.this);
dialog.setMessage("Loading ...");
dialog.setCancelable(true);
dialog.setInverseBackgroundForced(false);
myVideoView.setVideoURI( Uri.parse( "http://..." ) );
Related
String fileName = "android.resource://" + getPackageName() + "/raw/oryx1001";
MediaController videoMediaController = new MediaController(this);
//mVideoView.setVideoPath( Uri.parse());
mVideoView.setVideoURI(Uri.parse(fileName));
mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return true;
}
});
videoMediaController.setVisibility(View.GONE);
videoMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(videoMediaController);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//jump();
//System.out.println("dksadalkdakldsadlkadklsad");
mp.reset();
String fileName = "android.resource://" + getPackageName() + "/raw/oryx1001";
MediaController videoMediaController = new MediaController(MainActivity.this);
mVideoView.setVideoURI(Uri.parse(fileName));
mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return true;
}
});
videoMediaController.setVisibility(View.GONE);
videoMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(videoMediaController);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
}
});
simply my code 3gp file not working. it works on android oreo but not on lolipop device or samsung s6. is it api thing, or device thing for codecs supported? i tried everything mp4, 3gp nothin working.
E/MediaPlayer: Error (-38,0)
D/VideoView: Error: -38,0
/MediaPlayer: Error (1,-38)
You need to call mediaPlayer.start() in the onPrepared method by using a listener. You are getting this error because you are calling mediaPlayer.start() before it has reached the prepared state.
You have set setOnPreparedListener
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
but you start wrong object mVideoView.start(); you should call
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mp.start();//Change is hare ..
}
});
I'm using Video view to stream an HLS stream from wowza media server. I want to show the overall buffering just as in YouTube player, what i did is this and I'm able to access seek-bar object and i debugged the code it's not null
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
int topContainerId = getResources().getIdentifier("mediacontroller_progress", "id", "android");
seekbar = (SeekBar) mediaController.findViewById(topContainerId);
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if (percent<seekbar.getMax()) {
seekbar.setSecondaryProgress(percent);
}
}
});
}
});
The problem is i can't see any secondary progress in Media controller seekbar. If any clarification required required please write in comments. Help is really appreciated.
Use below code and you're goog to go
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
int topContainerId = getResources().getIdentifier("mediacontroller_progress", "id", "android");
seekbar = (SeekBar) mediaController.findViewById(topContainerId);
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if (percent<seekbar.getMax()) {
seekbar.setSecondaryProgress(percent/100);
}
}
});
}
});
i am displaying images and video in imageview and videoview but the issue is when video is
playing onpreparedlistener called but when video finish oncompletion listener not called
when videoview complete i increment the i for next video or images
also it gives me error in logcat like this but video is playing
10-29 20:12:47.770: E/MediaPlayer(3975): error (1, -2147483648)
private void nextVideo(String path){
mImageview.setVisibility(View.GONE);
if(mVideoview.getVisibility()==View.GONE){
mVideoview.setVisibility(View.VISIBLE);
}
controller = new MediaController(HomeActivityNewViewPager.this);
mVideoview.setVideoURI(Uri.parse(path));
mVideoview.setMediaController(null);
controller.setMediaPlayer(mVideoview);
mVideoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
long duration = mVideoview.getDuration();
second=duration;
//handler.removeCallbacks(runnable);
//handler.postDelayed(runnable,second);
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.v("video view completed","---"+i);
mp.reset();
if(automode){
if(i==myplaylistlocal.size() || i>myplaylistlocal.size())
{
String checkcount=spreferences.getString("roundcount", "");
Log.v("roundcount==Before Integer.parseInt","---->"+roundcount);
if(roundcount>=Integer.parseInt(checkcount))
{
roundcount=0;
Log.v("roundcount==After Integer.parseInt","---->"+roundcount);
updateplaylist();
}
i=0;
indexplus();
imagesautomode();
i++;
}
else if(i==myplaylistlocal.size()-1)
{
imagesautomode();
i++;
}
else{
imagesautomode();
}
}
else{
i++;
images();
}
}
});
mVideoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.v("Error in video playing","----->"+i);
return true;
}
});
}
Either way, the error referenced above is MEDIA_ERROR_UNKNOWN. If this video was made for this app, I would make sure that it is properly encoded for Android. Also make sure that is clearly defines its endpoint.
http://developer.android.com/reference/android/media/MediaPlayer.html#MEDIA_ERROR_UNKNOWN
this is a work around but could possbly work in your situation:
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if(what == MediaPlayer.MEDIA_ERROR_UNKNOWN)
//ERROR UNKNOWN - COULD BE IMPROPERLY FORMATTED VIDEO {
//MOVE ON TO NEXT VIDEO
//DO LOGGING
}
}
I have an application that show a streaming rtsp. How can I check if it ends?
Buffer empty? Anything else?
Socket is closed
Anything on control channel from media server?
private VideoView vidView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vidView = (VideoView) findViewById(R.id.myVideo);
uri = Uri.parse(RTSP_URL);
vidView.setMediaController(new MediaController(this));
vidView.setVideoURI(uri);
vidView.requestFocus();
vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Runnable runnable = new Runnable() {
#Override
public void run() {
vidView.start();
}
};
runOnUiThread(runnable);
}
});
vidView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("Error ", what + " " + extra);
return false;
}
});
vidView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d(getClass().getName(), " callback COMPLETE HERE ");
}
});
}
I don't find any solution to show a loading animation (or just an imageView) before a VideoView start to play : during the video is buffering.
Does anyone have a clue ?
Thanks.
Or you can just use Progress Dialog;
public class VideoEkrani extends Activity {
VideoView ekran;
String kaynakYolu = "http://commonsware.com/misc/test2.3gp";
ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sayfa_video_goruntule);
ekran = (VideoView) findViewById(R.id.videoViewESPU);
ekran.setMediaController(new MediaController(this));
ekran.setVideoURI(Uri.parse(kaynakYolu));
ekran.requestFocus();
ekran.start();
progDailog = ProgressDialog.show(this, "Please wait ...", "Retrieving data ...", true);
ekran.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
progDailog.dismiss();
}
});
}
}
Finally I found the method setOnPreparedListener() on VideoView.
I added a ProgressBar inside my layout, and hide it with the OnPreparedListener
You cant detect the video buffer using OnBufferingUpdateListener on MediaPlayer. If you use VideView set that listener inside OnPreparedListener.
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if(percent == 100){
//video have completed buffering
}
}
});
}
});