如何在播放之前/之后获得视频时长

时间:2014-06-03 11:44:47

标签: android

videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
            {
              @Override
              public void onCompletion(MediaPlayer mp) 
              {
                  VideoView videoView = (VideoView) findViewById(R.id.videoView1);
                  long duration= videoView.getDuration();

                  Toast.makeText(getApplicationContext(), "Video completed", Toast.LENGTH_LONG).show();
                  mShimmerDevice.stopStreaming();   
                  mShimmerDevice.stop();

              }

            });
       }

虽然此代码仅在视频播放完毕后才有效

3 个答案:

答案 0 :(得分:2)

您可以使用MediaMetadataRetriever。这样,您可以在不加载/播放视频的情况下检索有关视频的信息。

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(your_data_source);
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong( time );

答案 1 :(得分:0)

您可以在onPrepared()方法内进行视频开始播放之前获取视频的持续时间:

    //called when the mediaplayer is prepared to start buffering
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            Log.i("tag","video length:" +mediaPlayer.getDuration());

                }
    });

答案 2 :(得分:0)

您还可以使用FFmpegMediaMetadataRetriever

FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
retriever.setDataSource(your_data_source);
String time = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong( time );