Android AppCompatActivity 中的 YouTubePlayerView

Android YouTubePlayerView in AppCompatActivity

我正在实施 YouTube Android Player API 并尝试根据 this manual 将 YouTubePlayerView 添加到我的 AppCompatActivity,但出现异常:

Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity  which extends YouTubeBaseActivity as its context.

YouTubeBaseActivity 扩展了 Activity(不是 AppCompatActivity)。我检查了 sample project on GitHub 并注意到最新的提交是 3 年前的,所以我认为这个库已被弃用,特别是考虑到它仅作为 .jar 文件分发。 我的下一个问题是:我在哪里可以找到这个库的源代码来修复 YouTubeBaseActivity 或者我应该使用什么来代替它?或者,也许您可​​以建议我更好地在我的应用程序中播放 YouTube 视频(无意图)?谢谢

另一种方法是使用 YouTubePlayerFragment

A fragment that contains a YouTubePlayerView. Using this fragment is the preferred way of playing YouTube videos because your activity does not need to extend an activity provided by the library, as is the case with using the YouTubePlayerView directly.

看看这是否对您有帮助:
https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerFragment