如同所有Google的sdk ,必須申請 developer key
在取得key之後,使用下方的sample code 就可簡單的使用 youtube sdk
public class MainActivity extends YouTubeFailureRecoveryActivity implements PlaybackEventListener { YouTubePlayer mYouTubePlayer; YouTubePlayerView youTubeView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view); youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this); } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { if (!wasRestored) { player.cueVideo("wKJ9KzGQq0w"); } mYouTubePlayer = player; player.setPlayerStyle(PlayerStyle.MINIMAL); player.setPlaybackEventListener(this); } @Override public void onBackPressed() { // TODO Auto-generated method stub mYouTubePlayer.setFullscreen(false); } @Override protected YouTubePlayer.Provider getYouTubePlayerProvider() { return (YouTubePlayerView) findViewById(R.id.youtube_view); } @Override public void onBuffering(boolean arg0) { mYouTubePlayer.setFullscreen(true); } @Override public void onPaused() { // TODO Auto-generated method stub } @Override public void onPlaying() { // TODO Auto-generated method stub } @Override public void onSeekTo(int arg0) { // TODO Auto-generated method stub } @Override public void onStopped() { // TODO Auto-generated method stub } }
其中 YouTubeFailureRecoveryActivity 為 youtube sample code 所附加的檔案
youtube 的 sample code 可以在此下載
在這裡要特別提及的重點是,當使用fullscreen全螢幕撥放時,
很多人都會發現無法恢復成portrait mode
要恢復成portrait mode 其實很簡單,
注意我在 onbackpress做的事情,
當backpress時我將fullscreen mode設為false 這樣就能恢復成portrait mode了
附上sample code 供參考
沒有留言:
張貼留言