Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: attempt to call getduration without a valid mediaplayer

Tags:

android

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();
like image 501
trivektor Avatar asked Apr 19 '11 02:04

trivektor


3 Answers

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}
like image 110
V1 Kr Avatar answered Oct 31 '22 15:10

V1 Kr


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

                   }
               });
like image 21
Joe Avatar answered Oct 31 '22 15:10

Joe


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();
like image 21
Atma Avatar answered Oct 31 '22 15:10

Atma