如何禁用YouTubePlayerView上的点击?

时间:2017-06-17 12:24:05

标签: android youtube-api

我想在我的应用上实施YouTube播放器视图,但由于我的应用是闹钟,我不希望用户能够暂停/停止视频或更改音量。我试图在YouTube播放器视图上设置一个视图以阻止其点击,但我认为它违反了Google政策,因为当我这样做时,视频无法播放。我也尝试过:

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

但它没有用。有什么我可以做的吗?

2 个答案:

答案 0 :(得分:2)

使用this solution,您可以为每个孩子设置enablefalseview.setEnabled(false),从而停用视频及其所有子女的点击事件。在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();
        }
    }
}

答案 1 :(得分:0)

只需添加此行

youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);