Android: trying to call getduration without a valid media player

I am using the code below to play MP4 video (H.264 codecs, AAC) from a URL (URL is fine, no redirect, 404 or something else). However, I continue to receive errors "attempt to call getduration without a valid media player" or ERROR / MediaPlayer (382): error (1, -2147483648). Does anyone know how to fix this? Thanks

VideoView video = (VideoView) findViewById(R.id.myvideo); Intent videoint=getIntent(); String url = videoint.getStringExtra("url"); //The url pointing to the mp4 video.setVideoPath(url); video.requestFocus(); video.setMediaController(new MediaController(this)); video.start(); 
+8
android
source share
3 answers

Get the duration with the onPrepared callback enabled ... this will ensure that the video loads correctly before you try to get it.

 final VideoView video = (VideoView) findViewById(R.id.videoplayer); final MediaController controller = new MediaController(this); video.setVideoURI(Uri.parse(getIntent().getStringExtra("url"))); video.setMediaController(controller); controller.setMediaPlayer(video); video.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer mp) { int duration = video.getDuration(); video.requestFocus(); video.start(); controller.show(); } }); 
+22
source share

Make sure that you install all the controls that interact with the media planner after setting it up and preparing it. For example:

  mediaPlayer.setDataSource(dataSource); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(this); mediaPlayer.prepare(); progressBar = (ProgressBar) findViewById(R.id.progbar); progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setProgress(0); progressBar.setMax(mp.getDuration()); mediaPlayer.start(); 
+1
source share

In my case, the problem was with the search question. In my Service class, I changed:

 @Override public void onPrepared(MediaPlayer mp) { mp.start();} public getDur() { return mediaPalyer.getDuration} 

to

 int dr; //at the top inside Service class @Override public void onPrepared(MediaPlayer mp) { mp.start(); dr = player.getDuration();} public getDur() { return dr} 
0
source share

All Articles