Last active
August 29, 2015 14:24
-
-
Save William-ST/bdb75924eed97abc8c5e to your computer and use it in GitHub Desktop.
YoutubePlayerFragment
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
//make project | |
//compile 'com.google.android.gms:play-services:7.5.0' | |
//add YoutubeAndroidPlayerApi.jar | |
package whatdafaqshow.mox.moxapp.activities; | |
import android.content.Intent; | |
import android.graphics.Color; | |
import android.support.design.widget.CollapsingToolbarLayout; | |
import android.support.design.widget.CoordinatorLayout; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v4.widget.NestedScrollView; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.util.DisplayMetrics; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.webkit.JavascriptInterface; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import android.widget.Button; | |
import android.widget.LinearLayout; | |
import android.widget.ProgressBar; | |
import android.widget.TextView; | |
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.YouTubePlayerFragment; | |
import com.google.android.youtube.player.YouTubeStandalonePlayer; | |
import whatdafaqshow.mox.moxapp.R; | |
import whatdafaqshow.mox.moxapp.utils.UtilFonts; | |
public class YoutubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{ | |
public static final String API_KEY = "AIzaSyB6GeqAbVFvtaPx9qhEZjEvibSqxHBbOMU"; | |
public static final String VIDEO_ID = "mXsQXEIL6zY"; | |
private YouTubePlayer youTubePlayer; | |
private YouTubePlayerFragment youTubePlayerFragment; | |
private TextView textVideoLog; | |
private Button btnViewFullScreen; | |
private static final int RQS_ErrorDialog = 1; | |
private MyPlayerStateChangeListener myPlayerStateChangeListener; | |
private MyPlaybackEventListener myPlaybackEventListener; | |
String log = ""; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_youtube); | |
youTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager() | |
.findFragmentById(R.id.youtubeplayerfragment); | |
youTubePlayerFragment.initialize(API_KEY, this); | |
textVideoLog = (TextView)findViewById(R.id.videolog); | |
myPlayerStateChangeListener = new MyPlayerStateChangeListener(); | |
myPlaybackEventListener = new MyPlaybackEventListener(); | |
btnViewFullScreen = (Button)findViewById(R.id.btnviewfullscreen); | |
btnViewFullScreen.setOnClickListener(new View.OnClickListener(){ | |
@Override | |
public void onClick(View arg0) { | |
youTubePlayer.setFullscreen(true); | |
}}); | |
Button btnPlay = (Button) findViewById(R.id.btnplay); | |
btnPlay.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(youTubePlayer.isPlaying()) | |
youTubePlayer.pause(); | |
else | |
youTubePlayer.play(); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_splash_menu, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
int id = item.getItemId(); | |
Log.e("id", id+""); | |
switch(item.getItemId()){ | |
case R.id.action_share: | |
break; | |
case 16908332: | |
onBackPressed(); | |
break; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youtube, boolean b) { | |
youTubePlayer = youtube; | |
Toast.makeText(getApplicationContext(), "YouTubePlayer.onInitializationSuccess()", Toast.LENGTH_LONG).show(); | |
youtube.setPlayerStateChangeListener(myPlayerStateChangeListener); | |
youtube.setPlaybackEventListener(myPlaybackEventListener); | |
if (!b) { | |
youTubePlayer.cueVideo(VIDEO_ID); | |
} | |
} | |
@Override | |
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { | |
if (youTubeInitializationResult.isUserRecoverableError()) { | |
youTubeInitializationResult.getErrorDialog(this, RQS_ErrorDialog).show(); | |
} else { | |
Toast.makeText(this,"YouTubePlayer.onInitializationFailure(): " + youTubeInitializationResult.toString(),Toast.LENGTH_LONG).show(); | |
} | |
} | |
private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener { | |
private void updateLog(String prompt){ | |
log += "MyPlayerStateChangeListener" + "\n" +prompt + "\n\n====="; | |
textVideoLog.setText(log); | |
}; | |
@Override | |
public void onAdStarted() { | |
updateLog("onAdStarted()"); | |
} | |
@Override | |
public void onError( | |
com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0) { | |
updateLog("onError(): " + arg0.toString()); | |
} | |
@Override | |
public void onLoaded(String arg0) { | |
updateLog("onLoaded(): " + arg0); | |
} | |
@Override | |
public void onLoading() { | |
updateLog("onLoading()"); | |
} | |
@Override | |
public void onVideoEnded() { | |
updateLog("onVideoEnded()"); | |
} | |
@Override | |
public void onVideoStarted() { | |
updateLog("onVideoStarted()"); | |
} | |
} | |
private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener { | |
private void updateLog(String prompt){ | |
log += "MyPlaybackEventListener" + "\n-" + | |
prompt + "\n\n====="; | |
textVideoLog.setText(log); | |
}; | |
@Override | |
public void onBuffering(boolean arg0) { | |
updateLog("onBuffering(): " + String.valueOf(arg0)); | |
} | |
@Override | |
public void onPaused() { | |
updateLog("onPaused()"); | |
} | |
@Override | |
public void onPlaying() { | |
updateLog("onPlaying()"); | |
} | |
@Override | |
public void onSeekTo(int arg0) { | |
updateLog("onSeekTo(): " + String.valueOf(arg0)); | |
} | |
@Override | |
public void onStopped() { | |
updateLog("onStopped()"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment