我没有运气让 youtube-api 在 android 上工作。我正在努力获得最低限度的工作 - 只需通过公共(public)访问(无 oauth)播放视频。我有一个来自谷歌的 key ,我在我的模拟器上安装了 youtube 应用程序。我非常有信心这是正确的,因为我从 youtube api 示例代码创建了一个新项目,并且它运行良好。
虽然从我的项目运行时我得到:
E/YouTubeAndroidPlayerAPI: Error creating YouTubePlayerView
com.google.android.youtube.player.internal.w$a: Exception thrown by invoked constructor in com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer
...
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
...
Caused by: java.lang.IllegalArgumentException: The concrete class implementing IObjectWrapper must have exactly *one* declared private field for the wrapped object. Preferably, this is an instance of the ObjectWrapper<T> class.
at zst.a(SourceFile:76)
我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textAppearance="@android:style/TextAppearance.Small"
android:gravity="center"
android:text="Youtube!"/>
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
还有我的 Activity :
public class EducationVideoActivity extends YouTubeFailureRecoveryActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_education_youtube);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(YoutubeKey.DEVELOPER_KEY, this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("nCgQDjiotG0");
}
}
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
}
}
我在网上尝试了其他几个示例,它们都给出了相同的错误。欢迎提出任何建议!
最佳答案
我在荣耀 8 上遇到了一些问题。
我第一次以这种方式导入 YouTubeAndroidPlayerApi.jar -> 右键单击项目中的应用程序文件夹 -> 新建 -> 模块 -> 导入 .JAR/.AAR 包 -> 选择 YouTubeAndroidPlayerApi.jar -> 完成。然后打开项目结构并在 Dependencies 选项卡中添加 Module 依赖项。在这个 gradle 完成后,在 build.gradle 中是 compile project(':YouTubeAndroidPlayerApi')
。这样就发生了创建 YouTubePlayerView 的错误。
所以我还原了所有内容并像这样导入了 lib -> 将 YouTubeAndroidPlayerApi.jar 复制到/app/libs 文件夹 -> 打开项目结构,而不是模块依赖,我选择了 Jar 依赖,并从 libs 文件夹中选择了 YouTubeAndroidPlayerApi.jar。在 gradle 完成之后,现在在 build.gradle 中是 compile files('libs/YouTubeAndroidPlayerApi.jar')
并且一切正常。
作为mike gradle中已经提到应该是compile files('libs/YouTubeAndroidPlayerApi.jar')
而不是compile project(':YouTubeAndroidPlayerApi')
。
希望对您有所帮助。
关于java - 创建 YouTubePlayerView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42450953/