gpt4 book ai didi

android - 如何禁止点击 YouTubePlayerView?

转载 作者:搜寻专家 更新时间:2023-11-01 09:34:22 25 4
gpt4 key购买 nike

我想在我的应用程序上实现 YouTube 播放器 View ,但由于我的应用程序是一个闹钟,我不希望用户能够暂停/停止视频或更改音量。我试图在 YouTube 播放器 View 上放置一个 View 以阻止对其的点击,但我想这违反了谷歌政策,因为当我这样做时视频没有播放。我也试过:

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/ypvAlert"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/half_activity_vertical_margin"
android:layout_marginBottom="@dimen/standard_button_size"
android:clickable="false" />

但它不起作用。有什么我可以做的吗?

最佳答案

this solution ,您可以通过将每个子项的 enable 设置为 falseview.setEnabled(false) 来禁用 View 及其所有子项的点击事件。在 onInitializationSuccess 回调中调用它:

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
enableDisableView(mYoutubeplayerView, false);
mPlayer = youTubePlayer;
mPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
mPlayer.loadVideo("63kmMcHBQlA");
}

这是一个完整的工作示例:

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

String mApiKey = "YOUR_API_KEY";

YouTubePlayerView mYoutubeplayerView;

YouTubePlayer mPlayer;

/**
* disable all click event : https://stackoverflow.com/a/19464718/2614364 by Parag Chauhan
*
* @param view
* @param enabled
*/
public static void enableDisableView(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int idx = 0; idx < group.getChildCount(); idx++) {
enableDisableView(group.getChildAt(idx), enabled);
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mYoutubeplayerView = (YouTubePlayerView) findViewById(R.id.ypvAlert);
mYoutubeplayerView.initialize(mApiKey, this);
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
enableDisableView(mYoutubeplayerView, false);
mPlayer = youTubePlayer;
mPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
mPlayer.loadVideo("63kmMcHBQlA");
}

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


@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (resultCode == RESULT_OK) {
mPlayer.release();
}
}
}

关于android - 如何禁止点击 YouTubePlayerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44604612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com