有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    这个库对我来说很好,请检查:android-youtube-player

    在gradle文件中添加它的依赖项

    dependencies {
      implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:9.0.1'
    }
    

    为了开始使用播放器,你需要在布局中添加YouTubePlayerView

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayerView
            android:id="@+id/youtube_player_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    在代码中获取对YouTubePlayerView的引用并初始化它

    YouTubePlayerView youtubePlayerView = findViewById(R.id.youtube_player_view);
    getLifecycle().addObserver(youtubePlayerView);
    
    youtubePlayerView.initialize(new YouTubePlayerInitListener() {
        @Override
        public void onInitSuccess(@NonNull final YouTubePlayer initializedYouTubePlayer) {
            initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
                @Override
                public void onReady() {
                    String videoId = "6JYIGclVQdw";
                    initializedYouTubePlayer.loadVideo(videoId, 0);
                }
            });
        }
    }, true);
    

    这就是你所需要的,YouTube视频正在你的应用程序中播放