我正在尝试加载youtube的视频并播放它,但是它显示出具有错误代码400的网络错误。我正在使用you tube player(您可以在XML中看到),我已经在开发人员控制台中注册了该应用程序并遵循说明,但是有时加载后,它显示"发生错误"不知道我缺少什么。谢谢
public class AboutDescriptionActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private YouTubePlayerView youTubeView;
Toolbar toolbar;
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_description);
toolbar = (Toolbar) findViewById(R.id.aboutscreentoolbar);
toolbar.setTitle("About");
youTubeView = (YouTubePlayerView) findViewById(R.id.videoviewabout);
youTubeView.initialize(Config.DEVELOPER_KEY,this);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.backarrowicon));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//What to do on back clicked
onBackPressed();
}
});
}
@Override
public void onBackPressed() {
finish();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
player.cueVideo("https://youtu.be/CMvxh_uXEUc",1);
player.play();
// Hiding player controls
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), 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){
getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.videoviewabout);
}
}
我的XML文件是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.simpalm.peacenow_android.AboutDescriptionActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/aboutscreentoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ToolbarStyle"
app:titleTextColor="#ffffff"/>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/aboutscreentoolbar"
android:fillViewport="false"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/abouttextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/aboutscreentoolbar"
android:layout_marginTop="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/Abouttext"
android:textSize="18sp"
android:textStyle="bold"/>
<com.google.android.youtube.player.YouTubePlayerView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/videoviewabout">
</com.google.android.youtube.player.YouTubePlayerView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
而不是通过完整的URL通过YouTube videoid
而不是此
player.cueVideo("https://youtu.be/CMvxh_uXEUc",1);
尝试此
player.cueVideo("CMvxh_uXEUc",1);