YouTube播放器自定义应用程序强制每次关闭

时间:2019-05-07 23:12:27

标签: java android youtube-api

我现在正在尝试使用youtube自定义播放器。我正在尝试播放包含某个游戏原始音轨的播放列表的视频。

问题是,一切都由本书完成(再次),但是每次尝试打开APK时,我仍然遇到强制停止的问题。这是代码:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="20dp"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <view
        android:id="@+id/YoutubePlayView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/buttonPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Play Video" />

</LinearLayout>

Java:

package my.videoplayerapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";

    YouTubePlayerView mYoutubePlayerView;
    Button PlayVidButton;
    YouTubePlayer.OnInitializedListener mOnInitializedListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: Starting.");
        PlayVidButton = findViewById(R.id.buttonPlay);
        mYoutubePlayerView = findViewById(R.id.YoutubePlayView);

        mOnInitializedListener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                Log.d(TAG, "onClick: Initialization Done.");

                youTubePlayer.loadVideo("DBToMwdYBME");
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                Log.d(TAG, "onClick: Initialization Failed.");
            }
        };

        PlayVidButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: Initializing Youtube Player.");
                mYoutubePlayerView.initialize((YoutubeVidPlayerConfig.getApiKey()), mOnInitializedListener);
                Log.d(TAG, "onClick: Initialization Done.");
            }
        });
    }
}

Logcat,已链接:https://drive.google.com/open?id=1w5TfCIfyCWCrOZmrjb0lzTpndoQc2SJ0

我实际上是想通过按钮来播放视频,我知道播放下一个视频甚至播放视频序列的方法,我只是不知道为什么当我说正确的时候它一直崩溃现在...

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您有一个ID为“ YoutubePlayView”的视图,但您的类称为“ YoutubePlayerView”。更改其中之一,它应该可以工作(当然,不要强行关闭该位置)。