编程语言
首页 > 编程语言> > java-如何添加点击以播放/暂停到VideoView?

java-如何添加点击以播放/暂停到VideoView?

作者:互联网

我正在尝试将视频播放/暂停功能添加到正在进行的聊天中,因此我有一个标准:

VideoView vidRight;
vidRight = v.findViewById(R.id.videoViewRight);

但是,由于我需要声明已暂停的final,因此无法尝试添加轻击来与听众一起播放/暂停.

boolean paused = false;
vidRight.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (paused) {
            vidRight.start();
            paused = false;
        }
        else {
            vidRight.stopPlayback();
            paused = true;
        }

        return true;
    }
});

还有另一种方法吗?

解决方法:

将“已暂停”变量声明为成员变量,如下所示:

public class YourClassName extends AppCompatActivity implements YourInterfaces {
    private boolean paused=false;
    /* your other variables */
      @Override
    public void onCreate(Bundle savedInstanceState) {

        vidRight.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    if (paused) {
                        vidRight.start();
                        paused = false;
                    }
                    else {
                        vidRight.stopPlayback();
                        paused = true;
                    }

                    return true;
                }
            });

标签:android-videoview,java,android
来源: https://codeday.me/bug/20191024/1924675.html