YoutubePlayer没有播放视频并发出错误"网络错误(400)

时间:2017-10-04 13:32:15

标签: android youtube-api android-videoview

我正在尝试加载YouTube视频并播放它,但它显示网络错误,错误代码为400.我正在使用You tube播放器(你可以在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>

1 个答案:

答案 0 :(得分:0)

而不是传递完整的网址只传递YouTube VideoID

而不是

player.cueVideo("https://youtu.be/CMvxh_uXEUc",1);

试试这个

player.cueVideo("CMvxh_uXEUc",1);