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 に答える