如何通过AppCompatActivity使用YouTube Android Player API

时间:2018-06-24 18:14:30

标签: android android-youtube-api

要在我的应用中播放视频,我决定从YouTube Android Player API扩展。但是问题是我的菜单消失了,因为我没有从AppCompatActivity扩展。问题是:如何使用YouTube Android Player API并在应用程序中显示菜单?

public class TutorialsActivity extends YouTubeBaseActivity {

private YouTubePlayerView youTubePlayerView;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.youtube);

    youTubePlayerView = (YouTubePlayerView) findViewById(R.id.video1);
    youTubePlayerView.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.loadVideo("c9q88492aas");
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }
    });
}

XML

    <com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/video1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

当我从AppCompatActivity扩展时,它只是给我一个错误。

错误日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hyber.app/com.hyber.app.TutorialsActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.google.android.youtube.player.YouTubePlayerView

enter image description here

2 个答案:

答案 0 :(得分:5)

使用YouTubePlayerFragment代替使用YouTubePlayerView。正如文档所说: YouTubePlayerFragment是包含YouTubePlayerView的片段。使用此片段是播放YouTube视频的首选方式,因为您的活动不需要像直接使用YouTubePlayerView那样扩展库提供的活动。

查找更多信息here

活动代码:-

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;

public class MainActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {

    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        YouTubePlayerFragment youTubePlayerFragment =
                (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
        youTubePlayerFragment.initialize("api key",
                this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
        if (!wasRestored) {
            youTubePlayer.cueVideo("nCgQDjiotG0");
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        if (youTubeInitializationResult.isUserRecoverableError()) {
            youTubeInitializationResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format("There was an error initializing the YouTubePlayer (%1$s)", youTubeInitializationResult.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }


}

布局:-

<fragment
        android:id="@+id/youtube_fragment"
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:layout_width="0dp"
        android:layout_height="300dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

答案 1 :(得分:2)

使用App AppCompatDelegate

例如:

1)将AppCompatDelegate添加到您的活动中

public class MainActivity extends Activity implements AppCompatCallback {

    @Override
    public void onSupportActionModeStarted(ActionMode mode) {
      //let's leave this empty, for now
    }

    @Override
    public void onSupportActionModeFinished(ActionMode mode) {   
      // let's leave this empty, for now
    }

然后,在我们的Activity的onCreate()中:

i)使用AppCompatDelegate.create()创建AppCompatDelegate

ii)调用AppCompatDelegate.onCreate()(有一些Activity生命周期方法应委托给委托)

iii)使用AppCompatDelegate.setContentView()扩大布局

iv)使用AppCompatDelegate.setSupportActionbar()将工具栏添加到委托中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //let's create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)
    delegate = AppCompatDelegate.create(this, this);

    //we need to call the onCreate() of the AppCompatDelegate
    delegate.onCreate(savedInstanceState);

    //we use the delegate to inflate the layout
    delegate.setContentView(R.layout.activity_main);

    //Finally, let's add the Toolbar
    Toolbar toolbar= (Toolbar) findViewById(R.id.my_awesome_toolbar);
    delegate.setSupportActionBar(toolbar);
}

有关更多详细信息,

https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3

希望对您有帮助。