活动中的YouTube支持片段扩展了AppCompatActivity

时间:2019-05-31 17:12:41

标签: android

难以在应用中播放youtube视频,它只是显示空白屏幕。我无法扩展到任何youtube类,此活动必须保持扩展到AppCompactActivity。

例如,请参阅下面的代码段。

public class ActivityDetailsActivity extends AppCompatActivity {

private Button playButton;
private YouTubePlayer ytPlayer;

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

YouTubePlayerSupportFragment ytFragment =    YouTubePlayerSupportFragment.newInstance();
    ytFragment.initialize(getApiKey(), new   YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {

               playButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ytPlayer = youTubePlayer;
                        ytPlayer.loadVideo("ijXjCtCXcN4");
                        ytPlayer.play();
                    }
                });
            }


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

        }
    });

XML代码:

<fragment
    android:id="@+id/youtubePlayer"
    class="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/activityTitle" />

0 个答案:

没有答案