如何在YoutubePlayerView android中启用右键单击事件和左键单击事件
我正在使用youtube数据API在我的android应用程序中播放youtube视频。我想在用户单击youtubePlayerView右侧时将视频向前移动10秒(就像youtube应用程序一样),同样,在用户单击youtubePlayerView左侧时向后移动10秒。基本代码如下所示-如何在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
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();
}
}