开发者

Android: attempt to call getduration without a valid mediaplayer

I use the code below to play an MP4 video 开发者_Go百科(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();


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

                   }
               });


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


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}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜