带有计时器的Android Sync视频

时间:2011-01-25 11:03:52

标签: android timer sync android-videoview chronometer

我正在使用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();

2 个答案:

答案 0 :(得分:0)

不用担心,我找到了一种在计时器中使用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();

答案 1 :(得分:0)

我会给我两分钱,因为它可以帮助别人。

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

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

希望它有所帮助。