是android开发的新手。我试图实现youtube Api。但在我的XML文件中,它显示错误“无法实例化: - com.google.android.youtube.player.YouTubePlayerView“
这是我的主要活动代码: -
public class MainActivity extends YouTubeBaseActivity {
Button b;
private YouTubePlayerView youtubeplayerview;
private YouTubePlayer.OnInitializedListener onInitializedListener;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main);
youtubeplayerview=(YouTubePlayerView)findViewById(R.id.Youtube_view);
onInitializedListener=new YouTubePlayer.OnInitializedListener(){
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo("wvjqFy33HVA");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
};
b=(Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
youtubeplayerview.initialize("API KEY",onInitializedListener);
}
});
}
}
这是我的XML文件代码: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY VEDIO"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<com.google.android.youtube.player.YouTubePlayerView
android:layout_width="500dp"
android:layout_height="500dp"
android:id="@+id/Youtube_view"
android:layout_alignParentTop="true"
android:layout_above="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
答案 0 :(得分:1)
您是否已将YouTube Android Player API添加到图书馆?添加它可以解决我的问题。尝试按照以下步骤添加相同内容:
1.从https://developers.google.com/youtube/android/player/downloads/
下载YouTube Android Player API jar文件2.提取zip文件,转到libs - &gt; YouTubeAndroidPlayerApi.jar并将其复制
3.将jar文件粘贴到android studio
中的/ app / libs /目录最后将编译文件('libs / YouTubeAndroidPlayerApi.jar')添加到您的应用级build.gradle文件
同步,构建并检查它是否正常工作
答案 1 :(得分:-3)
你为android youtube播放器生成了api密钥吗? 链接:https://console.developers.google.com
如果是,您必须使用之前生成的api密钥更改此youtubeplayerview.initialize("API KEY",onInitializedListener);
中的“API密钥”。
无论如何,您可以尝试使用此类替换您的类,YoutubeDeveloperKey是您生成的密钥:
public class ActivityPlayYouTube extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "YOUR_DEVELOPER_KEY_GOES_HERE";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playyoutube);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.you_tube_api, menu);
return true;
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
/*
* Now that this variable YPlayer is global you can access it
* throughout the activity, and perform all the player actions like
* play, pause and seeking to a position by code.
*/
YPlayer.cueVideo("2zNSgSzhBfM");
}
}
}