【问题标题】:How to enable right click event and left click event in a YoutubePlayerView android如何在 YoutubePlayerView android 中启用右键单击事件和左键单击事件
【发布时间】:2019-10-20 09:49:44
【问题描述】:

我正在使用 youtube 数据 API 在我的 Android 应用中播放 youtube 视频。当用户单击 youtubePlayerView 的右侧(如 youtube 应用程序)时,我想将视频向前移动 10 秒,类似地,当用户单击 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) {

        }
    });

【问题讨论】:

  • 您提供的代码只是初始化 YouTube 播放器,并没有真正显示您尝试通过单击 youtubePlayerView 左侧来实现前进和后退功能的内容反之亦然。
  • 您需要自定义YouTubePlayerViewoverridedispatchTouchEvent(MotionEvent)onInterceptTouchEvent(MotionEvent)onTouchEvent(MotionEvent)的容器并检测点击,确定其相对于View中心的位置和执行所需的(前进/后退)操作。
  • 你能告诉我一些基本的代码怎么做吗?只是提示我需要。

标签: android youtube-api youtube-data-api


【解决方案1】:

在您的 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>

用另一个自定义 ViewGroup 把它包起来,就像这样

<!--
    Make sure to prepend the complete package of the `CustomViewGroup`.
-->
<CustomViewGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <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>

定义你的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();
    }
}

我还没有通过运行来验证代码,所以可能存在语法和编译器错误,但正如你要求的基本实现,应该这样做。

【讨论】:

    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 2013-06-28
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多