Android Studio 유투브 API 연동


에뮬레이터에 유튜부가 없기 때문에 기기에 연결해서 실행해보도록 한다.

1. 유튜브 안드로이드 플레이어 API 다운로드
https://developers.google.com/youtube/android/player/downloads/?hl=ko



2. 다운받은 파일을 해당 폴더에 넣는다.

3. project struture에서 Path 설정을 한다.


4. 구글 개발자 api 설정





SHA1 복사

SHA1 붙여넣기


패키지 복사
패키지 붙여넣기
레이아웃에 뷰 만들기

설정


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.example.minpa.tutorial7;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
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 {
 
    YouTubePlayerView youtubeView;
    Button button;
    YouTubePlayer.OnInitializedListener listener;
 
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        button = (Button) findViewById((R.id.youtubeButton));
        youtubeView = (YouTubePlayerView) findViewById(R.id.youtubeView);
        listener = new YouTubePlayer.OnInitializedListener(){
 
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                // 비디오 아이디
                youTubePlayer.loadVideo("vewH-f3fAes&index=7&list=PLRx0vPvlEmdB6sCgj_jubp8KPb1ni0VOC");
            }
 
            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
 
            }
        };
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // api 키 값
                youtubeView.initialize("AIzaSyAVft09gsOBxS2mOGYh-cj6p_Kq5TT8RbQ", listener);
            }
        });
 
    }
}
 
cs

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.minpa.tutorial7">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
cs

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.minpa.tutorial7.MainActivity">
 
 
    <view
        android:id="@+id/youtubeView"
        class="com.google.android.youtube.player.YouTubePlayerView"
        id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.064" />
 
    <Button
        android:id="@+id/youtubeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="재생"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.576" />
</android.support.constraint.ConstraintLayout>
cs

댓글