0

我正在使用 Android 使用VideoView构建视频播放器。我已经设法让视频播放器运行,现在我正在使用计时器设置一个计数器,当我选择文件时它开始滴答作响。
但是,视频剪辑需要几秒钟才能开始运行,而计时器在剪辑开始时已经开始计时几秒钟。

如何编码以将计数器与媒体文件同步?
我已经检查过,但似乎无法找到答案。

    video = (VideoView) findViewById(R.id.video);
    play = (Button) findViewById(R.id.play);
    pause = (Button) findViewById(R.id.pause);
    stop = (Button) findViewById(R.id.stop);
    reset = (Button) findViewById(R.id.reset);
    Chronometer counter = (Chronometer) findViewById(R.id.chrono);
    startTime = SystemClock.elapsedRealtime();
    time = (TextView) findViewById(R.id.time);

        try {
        video.setVideoPath(link);
        video.start();
        video.requestFocus();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

    play.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            video.start();
            video.requestFocus();
        }
    });
    pause.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            video.pause();
        }
    });
    stop.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                video.stopPlayback();
                video.setVideoPath(link);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
    });
    reset.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                video.seekTo(0);
                video.start();
                video.requestFocus();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
    });

counter.setOnChronometerTickListener(new OnChronometerTickListener(){
        public void onChronometerTick(Chronometer arg0) {
            countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
            duration = video.getDuration();
            if (countUp % 60 <= 9) {
                countText = (countUp / 60) + ":0" + (countUp % 60);
            } else {
                countText = (countUp / 60) + ":" + (countUp % 60);           
            } 
            if (duration % 60000 <= 9) {
                durationText = (duration / 60000) + ":0" + (duration % 60000);
            } else {
                durationText = (duration / 60000) + ":" + (duration % 60000);           
            }
            time.setText(countText + " / " + durationText);
        }
    });
    counter.start();
4

2 回答 2

0

不用担心,我找到了在 Chronometer中使用 VideoView getCurrentPosition()的方法。现在它总能看到正确的当前时间并且更容易保持检查。

counter.setOnChronometerTickListener(new OnChronometerTickListener(){
        public void onChronometerTick(Chronometer arg0) {
            countUp = video.getCurrentPosition();
            countUp = Math.round(countUp / 1000);
            duration = video.getDuration();
            duration = duration / 1000;
            long a = (long) countUp;
            if (countUp % 60000 <= 9) {
                countText = (a / 60000) + ":0" + (a % 60000);
            } else {
                countText = (a / 60000) + ":" + (a % 60000);           
            } 
            if (duration % 60000 <= 9) {
                durationText = (duration / 60000) + ":0" + (duration % 60000);
            } else {
                durationText = (duration / 60000) + ":" + (duration % 60000);           
            }
            time.setText(countText + " / " + durationText);
        }
    });
    counter.start();
于 2011-01-26T08:47:32.363 回答
0

我会为此付出两分钱,因为它可能会帮助其他人。

您可以设置 onPreparedListener,您可以在其中获取 MediaPlayer。MediaPlayer 具有 onInfoListener ,它可以让您知道媒体何时被缓冲,甚至是实际渲染的第一帧 MEDIA_INFO_VIDEO_RENDERING_START(仅限 API 17+)。我正在使用它来更新覆盖的 VideoView 类中的本地状态变量(类似于:枚举 VideoPlayerState (BUFFERING, PLAYING, STOPPED, OPEN, PAUSED))。您可以覆盖其设置器以在状态更改时获取回调。该回调可以启动/暂停您的计时器。

setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(final MediaPlayer mp) {
            mp.setOnInfoListener(new OnInfoListener() {

                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
                        setPlayerState(VideoPlayerState.BUFFERING);
                    } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
                        if (mp.isPlaying()
                                && mPlayerState == VideoPlayerState.BUFFERING) {
                            setPlayerState(VideoPlayerState.PLAYING);
                        }
                    }
                    return false;
                }
            });
});

您还可以在 VideoView 上覆盖这些方法来切换状态:

@Override
public void pause() {
    super.pause();
    setPlayerState(VideoPlayerState.PAUSED);
}

@Override
public void start() {
    super.start();
    setPlayerState(VideoPlayerState.PLAYING);
}

@Override
public void stopPlayback() {
    super.stopPlayback();
    setPlayerState(VideoPlayerState.STOPPED);
}

希望能帮助到你。

于 2013-09-02T05:57:21.163 回答