Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在YoutubePlayerView android中启用右键单击事件和左键单击事件_Android_Youtube Api_Youtube Data Api - Fatal编程技术网

如何在YoutubePlayerView android中启用右键单击事件和左键单击事件

如何在YoutubePlayerView android中启用右键单击事件和左键单击事件,android,youtube-api,youtube-data-api,Android,Youtube Api,Youtube Data Api,我正在使用youtube数据API在我的android应用程序中播放youtube视频。我想在用户单击youtubePlayerView右侧时将视频向前移动10秒(就像youtube应用程序一样),同样,在用户单击youtubePlayerView左侧时向后移动10秒。基本代码如下所示- final YouTubePlayerView youTubePlayerView = findViewById(R.id.video_play_player_id); String videoUr

我正在使用youtube数据API在我的android应用程序中播放youtube视频。我想在用户单击youtubePlayerView右侧时将视频向前移动10秒(就像youtube应用程序一样),同样,在用户单击youtubePlayerView左侧时向后移动10秒。基本代码如下所示-

 final YouTubePlayerView youTubePlayerView = findViewById(R.id.video_play_player_id);

    String videoUrl = getIntent().getStringExtra("videoUrl");

    final String videoId = extractYTId(videoUrl);

    youTubePlayerView.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

            youTubePlayer.loadVideo(videoId);
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);

        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }
    });

在xml
layout
文件中,您将看到如下内容

<com.google.android.youtube.player.YouTubePlayerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ytPlayerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

</com.google.android.youtube.player.YouTubePlayerView>
定义您的
CustomViewGroup

public class CustomViewGroup extends ViewGroup {

    private boolean actionDownReceived = false;

    private final int DELTA_TOLERANCE = 70;
    private int actionDownX = -1;
    private int actionDownY = -1;

    public CustomViewGroup(Context context)
    {

    }


    public CustomViewGroup(Context context, AttributeSet attrSet)
    {

    }

    public CustomViewGroup(Context context, AttributeSet attrSet, int defStyleAttr)
    {

    }

    //  Now write the appropriate methods to capture touch events.
    @Override
    public boolean dispatchTouchEvent(MotionEvent event)
    {
        switch (event.getActionMasked())
        {
            case MotoinEvent.ACTION_DOWN:
                actionDownReceived = true;
                actionDownX = event.getRawX();
                actionDownY = event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                // Check if user moved his finger, there should also be a time check, i.e. if user pressed longer for a click. But I'll let you come up with it.
                if (Math.abs(event.getRawX() - actionDownX) >= DELTA_TOLERANCE) {
                    actionDownReceived = false;
                }
                else if (Math.abs(event.getRawY() - actionDownY) >= DELTA_TOLERANCE) {
                    actionDownReceived = false;
                }
                break;

            case MotionEvent.ACTION_UP:
                if (actionDownReceived) {
                    if (isPressOnLeftSide(event.getRawX(), event.getRawY()) {
                        // Your logic for rewind.
                    }
                    else if (isPressOnRight(event.getRawX(), event.getRawY()) {
                        // Your logic for forward.
                    }
                    actionDownReceived = false;
                }
                break;
            case MotionEven.ACTION_CANCEL: {
                actionDownReceived = false;
                break;
            }
        }
        super.dispatchTouchEvent();
    }
}

我还没有通过运行它来验证代码,因此可能会出现语法和编译器错误,但是当您要求基本实现时,这应该可以做到。

您提供的代码只是初始化YouTube播放器,并没有通过单击youtubePlayerView左侧来实际显示您试图实现的前进和后退功能,反之亦然。您需要自定义
youtubePlayerView
覆盖
dispatchTouchEvent(MotionEvent)
onInterceptTouchEvent(MotionEvent)
onTouchEvent(MotionEvent)
并检测单击,确定其相对于视图中心的位置,并执行所需的(向前/向后)操作。您能给我演示一些基本的代码吗?只是我需要的提示。
public class CustomViewGroup extends ViewGroup {

    private boolean actionDownReceived = false;

    private final int DELTA_TOLERANCE = 70;
    private int actionDownX = -1;
    private int actionDownY = -1;

    public CustomViewGroup(Context context)
    {

    }


    public CustomViewGroup(Context context, AttributeSet attrSet)
    {

    }

    public CustomViewGroup(Context context, AttributeSet attrSet, int defStyleAttr)
    {

    }

    //  Now write the appropriate methods to capture touch events.
    @Override
    public boolean dispatchTouchEvent(MotionEvent event)
    {
        switch (event.getActionMasked())
        {
            case MotoinEvent.ACTION_DOWN:
                actionDownReceived = true;
                actionDownX = event.getRawX();
                actionDownY = event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                // Check if user moved his finger, there should also be a time check, i.e. if user pressed longer for a click. But I'll let you come up with it.
                if (Math.abs(event.getRawX() - actionDownX) >= DELTA_TOLERANCE) {
                    actionDownReceived = false;
                }
                else if (Math.abs(event.getRawY() - actionDownY) >= DELTA_TOLERANCE) {
                    actionDownReceived = false;
                }
                break;

            case MotionEvent.ACTION_UP:
                if (actionDownReceived) {
                    if (isPressOnLeftSide(event.getRawX(), event.getRawY()) {
                        // Your logic for rewind.
                    }
                    else if (isPressOnRight(event.getRawX(), event.getRawY()) {
                        // Your logic for forward.
                    }
                    actionDownReceived = false;
                }
                break;
            case MotionEven.ACTION_CANCEL: {
                actionDownReceived = false;
                break;
            }
        }
        super.dispatchTouchEvent();
    }
}