Last active
February 15, 2019 12:32
-
-
Save sunmeat/9a447e8db4094c5fe79e607705f774c1 to your computer and use it in GitHub Desktop.
youtube video player example android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MainActivity.java: | |
package com.alex.youtube; | |
import android.os.Bundle; | |
import android.content.Intent; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.widget.Toast; | |
import com.google.android.youtube.player.YouTubeBaseActivity; | |
import com.google.android.youtube.player.YouTubeInitializationResult; | |
import com.google.android.youtube.player.YouTubePlayer; | |
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle; | |
import com.google.android.youtube.player.YouTubePlayerView; | |
public class MainActivity extends YouTubeBaseActivity implements | |
YouTubePlayer.OnInitializedListener { | |
private static final int RECOVERY_DIALOG_REQUEST = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, | |
WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
setContentView(R.layout.activity_main); | |
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view); | |
// Initializing video player with developer key | |
youTubeView.initialize(Config.DEVELOPER_KEY, this); | |
} | |
@Override | |
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) { | |
if (errorReason.isUserRecoverableError()) { | |
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); | |
} else { | |
Toast.makeText(this, "oops!", Toast.LENGTH_LONG).show(); | |
} | |
} | |
@Override | |
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { | |
if (!wasRestored) { | |
// loadVideo() will auto play video | |
// use cueVideo() method, if you don't want to play it automatically | |
player.loadVideo(Config.YOUTUBE_VIDEO_CODE); | |
// hiding player controls | |
player.setPlayerStyle(PlayerStyle.CHROMELESS); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == RECOVERY_DIALOG_REQUEST) { | |
// retry initialization if user performed a recovery action | |
getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this); | |
} | |
} | |
private YouTubePlayer.Provider getYouTubePlayerProvider() { | |
return (YouTubePlayerView) findViewById(R.id.youtube_view); | |
} | |
} | |
class Config { | |
// Google Console APIs developer key for YouTube | |
// replace this key with your's !!! | |
static final String DEVELOPER_KEY = "AIzaSyABYoczeHg4XABx_jMRfv-CqmA2YMsIY4A"; | |
// YouTube video id | |
static final String YOUTUBE_VIDEO_CODE = "_oEA18Y8gM0"; | |
} | |
===================================================================================================================== | |
скопировать в папку drawable такие файлы (из папки 82. camera) | |
- logo.png | |
- shadow.xml | |
- snake.jpg | |
- wild.png | |
===================================================================================================================== | |
activity_main.xml: | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ImageView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_centerInParent="true" | |
android:scaleType="centerCrop" | |
android:src="@drawable/snake" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:layout_marginLeft="30dp" | |
android:layout_marginRight="30dp" | |
android:orientation="vertical"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@drawable/shadow" | |
android:gravity="center_horizontal" | |
android:orientation="vertical"> | |
<com.google.android.youtube.player.YouTubePlayerView | |
android:id="@+id/youtube_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="30dp" /> | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="70dp" | |
android:layout_marginBottom="20dp" | |
android:scaleType="fitCenter" | |
android:src="@drawable/logo" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10dp" | |
android:text="YouTube player example" | |
android:textSize="20sp" | |
android:textStyle="bold" /> | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="40dp" | |
android:layout_marginBottom="30dp" | |
android:scaleType="fitCenter" | |
android:src="@drawable/wild" /> | |
</LinearLayout> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="40dp" | |
android:background="@drawable/shadow" | |
android:text="SKIP INTRO" /> | |
</LinearLayout> | |
</RelativeLayout> | |
===================================================================================================================== | |
AndroidManifest.xml: | |
... | |
<uses-permission android:name="android.permission.INTERNET"/> | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment