Android:尝试在没有有效媒体播放器的情况下调用getduration

Tri*_*ong 11 android

我使用下面的代码从URL播放MP4视频(H.264,AAC编解码器)(网址非常好,没有重定向,404或任何东西).但是,我不断收到错误"尝试在没有有效媒体播放器的情况下调用getduration"或ERROR/MediaPlayer(382):error(1,-2147483648).有谁知道如何解决它?谢谢

 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();
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 25

从onPrepared回调中检索持续时间...这将确保在您尝试获取视频的持续时间之前正确加载视频.

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

                   }
               });
Run Code Online (Sandbox Code Playgroud)

  • 新错误错误:VideoView上的(-38,0).任何的想法? (2认同)