4

我是 android 开发的新手。我正在尝试实现 youtube Api。但在我的 XML 文件中,它显示错误“无法实例化:-com.google.android.youtube.player.YouTubePlayerView”

这是我的 MAinActivity 代码:-

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" />

这是错误截图

4

2 回答 2

3

您是否将 YouTube Android Player API 添加到您的库中?添加它为我解决了这个问题..尝试按照下面给出的步骤添加相同的内容:

1.从https://developers.google.com/youtube/android/player/downloads/下载 YouTube Android Player API jar 文件

2.解压 zip 文件,进入 libs -> YouTubeAndroidPlayerApi.jar 并复制它

3.将jar文件粘贴到android studio中的/app/libs/目录下

最后将编译文件('libs/YouTubeAndroidPlayerApi.jar')添加到您的应用级 build.gradle 文件中

同步,构建并检查它是否工作正常

于 2018-01-22T04:17:48.307 回答
-3

您是否为 android youtube 播放器生成了 api 密钥?链接:https ://console.developers.google.com

youtubeplayerview.initialize("API KEY",onInitializedListener);如果是,您必须通过您之前生成的 api 密钥更改此处的“API KEY” 。

无论如何,你可以尝试用这个类替换你的类,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");
        }
    }
}
于 2016-02-26T08:52:54.277 回答