android

YouTube API 플레이어

디츠 2020. 2. 19. 15:31

* lib 폴더에 라이브러리 추가

YouTubeAndroidPlayerApi-1.2.2.zip
0.63MB

* activity_myplayer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

</LinearLayout>

* MyPlayerActivity.java

public class MyPlayerActivity extends YouTubeBaseActivity {

    public static final String API_KEY = "API_KEY";

    YouTubePlayerView youTubePlayerView;
    YouTubePlayer.OnInitializedListener listener;

    public PlayerActivity() {

    }

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

        Intent intent = getIntent();
        final ArrayList<String> idList = (ArrayList<String>) intent.getSerializableExtra("idList");

        youTubePlayerView = findViewById(R.id.youTubePlayerView);
        listener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.loadVideo(VIDEO_ID);
            }

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

            }
        };

        youTubePlayerView.initialize(API_KEY, listener);
    }

}