当视频到达当前时间Android时,在TextView中显示文本

时间:2014-01-17 16:34:25

标签: android android-videoview

我想在视频到达当前时间时在TextView中显示文本。我试过这段代码:

public class VideoActivity extends Activity {
    private static final String MY_AD_UNIT_ID = null;
    protected static final String TAG = null;
    private AdView adView;
    int waktu;
    Button btncek;
    private MediaController controller = null;
    TextView textView2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videoplayer);
    Bundle bundle = getIntent().getExtras();
    String urlbarus = getIntent().getStringExtra("urlbaru");
    final VideoView videoView = (VideoView) findViewById(R.id.videoView1);

    videoView.setVideoPath(urlbarus);


    MediaController.MediaPlayerControl mcEvents = new MediaController.MediaPlayerControl() {        
        public void start() {
            videoView.start();
        }

        public void seekTo(int pos) {
            videoView.seekTo(pos);         
        }

        public void pause() {
            videoView.pause();
        }

        public boolean isPlaying() {            
            return videoView.isPlaying();
        }

        public int getDuration() {          
            return videoView.getDuration();
        }

        public int getCurrentPosition() {           
            return videoView.getCurrentPosition();
        }

        public boolean canSeekForward() {           
            return true;
        }

        public boolean canSeekBackward() {
            return true;
        }

        public boolean canPause() {
            return true;
        }

        @Override
        public int getBufferPercentage() {
            // TODO Auto-generated method stub
            return 0;
        }
    };

    controller = new MediaController(this);
    controller.setMediaPlayer(mcEvents);
    controller.setAnchorView(findViewById(R.id.wrapper));

    videoView.start();


  if(mcEvents.getCurrentPosition()>=10000){
            textView2.setText("kebaca");
            }

@Override
public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    controller.show();
    return false;
}

这是显示文字的代码:

if(mcEvents.getCurrentPosition()>=10000){
    textView2.setText("kebaca");
}

我已尝试过该代码,但没有任何反应(textview2不显示文本)

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

尝试类似..

    final MediaController.MediaPlayerControl mcEvents = ....

    textView2.setText(""); // edited

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (mcEvents.getCurrentPosition()<10000){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ignored) { }
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView2.setText("kebaca");
                }
            });
        }
    }).start();