Skip to content

Instantly share code, notes, and snippets.

@huewu
Created March 29, 2013 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huewu/5271359 to your computer and use it in GitHub Desktop.
Save huewu/5271359 to your computer and use it in GitHub Desktop.
YouTubePlayerActivity
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
PlaylistItem item = (PlaylistItem) mListView.getItemAtPosition(position);
String videoId = item.getSnippet().getResourceId().getVideoId();
Intent intent = new Intent(MainActivity.this, YouTubePlayerActivity.class);
intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_ID, videoId);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
/**
* Copyright 2013 The Finest Artist
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.gdg_opensource_codelab_sample_1;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.widget.FrameLayout.LayoutParams;
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.OnFullscreenListener;
import com.google.android.youtube.player.YouTubePlayerView;
public class YouTubePlayerActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, OnFullscreenListener {
public static final String EXTRA_VIDEO_ID = "video_id";
private static final boolean TOAST = false;
private static final int RECOVERY_DIALOG_REQUEST = 1;
public static final String GOOGLE_API_KEY = MainActivity.YOUTUBE_API_KEY;
@SuppressLint("InlinedApi")
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
@SuppressLint("InlinedApi")
private static final int LANDSCAPE_ORIENTATION = Build.VERSION.SDK_INT < 9
? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
private YouTubePlayerView mPlayerView;
private String mVideoId = null;
private YouTubePlayer mPlayer = null;
private boolean mAutoRotation = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlayerView = new YouTubePlayerView(this);
mPlayerView.initialize(GOOGLE_API_KEY, this);
mVideoId = getIntent().getStringExtra(EXTRA_VIDEO_ID);
mAutoRotation = Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
addContentView(mPlayerView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
mPlayer = player;
if (mAutoRotation) {
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
| YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
| YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE
| YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
} else {
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
| YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
| YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
}
if (!wasRestored) {
player.loadVideo(mVideoId);
player.setOnFullscreenListener(this);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer (%1$s)",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@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(GOOGLE_API_KEY, this);
}
}
public YouTubePlayer.Provider getYouTubePlayerProvider() {
return mPlayerView;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (mPlayer != null)
mPlayer.setFullscreen(true);
if (TOAST)
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
if (mPlayer != null)
mPlayer.setFullscreen(false);
if (TOAST)
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
} else {
if (TOAST)
Toast.makeText(this, "configuration changed", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFullscreen(boolean fullsize) {
if (TOAST)
Toast.makeText(this, "full size change : " + fullsize, Toast.LENGTH_SHORT).show();
if (fullsize) {
setRequestedOrientation(LANDSCAPE_ORIENTATION);
} else {
setRequestedOrientation(PORTRAIT_ORIENTATION);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
AudioManager audioManager = (AudioManager) getBaseContext().getSystemService(
Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE | AudioManager.FLAG_SHOW_UI);
return true;
}
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
AudioManager audioManager = (AudioManager) getBaseContext().getSystemService(
Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE | AudioManager.FLAG_SHOW_UI);
return true;
}
return super.onKeyDown(keyCode, event);
}
}//end of class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment