11

I use the code below to play an MP4 video (H.264, AAC codecs) from a URL (The url is perfectly fine, no redirect, 404 or anything). However, I keep getting the errors "attempt to call getduration without a valid mediaplayer" or ERROR/MediaPlayer(382): error (1, -2147483648). Does anyone have any idea how to fix it? 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();
trivektor
  • 5,608
  • 9
  • 37
  • 53

3 Answers3

26

Retrieve the duration from the onPrepared callback...this will ensure the video is properly loaded before you attempt to get it's duration.

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();

                   }
               });
Joe
  • 4,801
  • 2
  • 16
  • 8
  • That's a rather generic error and could be any number of things. Edited above to reflect some new things you could try – Joe Apr 19 '11 at 03:59
  • @Joe what about MediaPlayer ? any suggestions how to resolve error for that? – Maveňツ Dec 05 '16 at 08:31
1

In my case the issue was with the seekbar. 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}
V1 Kr
  • 177
  • 1
  • 3
  • 18
1

Make sure you're setting all controls that interact with the mediaplayer after it is setup and prepared. 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();
Atma
  • 29,141
  • 56
  • 198
  • 299