使用YouTubePlayerSupportFragment在后台播放Youtube视频

时间:2014-12-17 09:53:54

标签: android youtube

我已成功使用YouTubePlayerSupportFragment播放YouTube视频。
但是当按下后退按钮时,我仍然需要继续在后台播放。
如果您有任何想法,请与我分享。谢谢!
这是我的代码 -


YoutubePlayerFragment类

public class YoutubePlayerFragment {
public static String DEVELOPER_KEY = "DEVELOPER_KEY";
public static boolean isFullScreen;

public static void setYoutubePlayerFragment(final Fragment fragment) {
    MainFragment.youtubePlayerFragment = new YouTubePlayerSupportFragment();
    MainFragment.youtubePlayerFragment.initialize(DEVELOPER_KEY, new OnInitializedListener() {

        @Override
        public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {

        }

        @Override
        public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
                boolean paramBoolean) {
            PlayerService.youTubePlayer = player;
            PlayerService.youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
            setListener(fragment.getActivity());
        }
    });
    replaceFragment(fragment);
}

public static void replaceFragment(final Fragment fragment) {
    if (fragment == null)
        return;
    FragmentManager fragmentManager = fragment.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.youtubeplayerFrameLayout,
            MainFragment.youtubePlayerFragment);
    fragmentTransaction.commit();
}

public static void setListener(final Activity activity) {
    PlayerService.youTubePlayer.setOnFullscreenListener(new OnFullscreenListener() {

        @Override
        public void onFullscreen(boolean isFullScreen) {
            YoutubePlayerFragment.isFullScreen = isFullScreen;
            Log.d(App.tagName, "setOnFullscreenListener : onFullscreen");
        }
    });

    PlayerService.youTubePlayer.setPlayerStateChangeListener(new PlayerStateChangeListener() {

        @Override
        public void onAdStarted() {
            Log.d(App.tagName, "setPlayerStateChangeListener : onAdStarted");
        }

        @Override
        public void onError(ErrorReason arg0) {
            Log.d(App.tagName, "setPlayerStateChangeListener : onError : " + arg0);
        }

        @Override
        public void onLoaded(String arg0) {
            Log.d(App.tagName, "setPlayerStateChangeListener : onLoaded");
        }

        @Override
        public void onLoading() {
            Log.d(App.tagName, "setPlayerStateChangeListener : onLoading");
        }

        @Override
        public void onVideoEnded() {
            Log.d(App.tagName, "setPlayerStateChangeListener : onVideoEnded");
        }

        @Override
        public void onVideoStarted() {
            Log.d(App.tagName, "setPlayerStateChangeListener : onVideoStarted");
        }
    });

    PlayerService.youTubePlayer.setPlaybackEventListener(new PlaybackEventListener() {

        @Override
        public void onBuffering(boolean arg0) {
            Log.d(App.tagName, "setPlaybackEventListener : onBuffering");
            if (MainActivity.isBack && PlayerService.youTubePlayer != null) {
                PlayerService.youTubePlayer.play();
            }
        }

        @Override
        public void onPaused() {
            Log.d(App.tagName, "setPlaybackEventListener : onPaused");
            PlayerFrameLayout.ibPlay.setImageResource(R.drawable.ic_play_arrow_white_36dp);
            if (MainActivity.isBack && PlayerService.youTubePlayer != null) {
                PlayerService.youTubePlayer.play();
            }
        }

        @Override
        public void onPlaying() {
            Log.d(App.tagName, "setPlaybackEventListener : onPlaying");
            PlayerFrameLayout.ibPlay.setImageResource(R.drawable.ic_pause_white_36dp);
        }

        @Override
        public void onSeekTo(int arg0) {
            Log.d(App.tagName, "setPlaybackEventListener : onSeekTo");
        }

        @Override
        public void onStopped() {
            Log.d(App.tagName, "setPlaybackEventListener : onStopped");             
        }
    });
}}

使用服务播放Youtube视频。

public class PlayerService extends Service {
public static YouTubePlayer youTubePlayer = null;
private int MSG;

@Override
public void onCreate() {
    super.onCreate();
    Log.d(App.tagName, "PlayerService : onCreate");
    YoutubePlayerFragment.setYoutubePlayerFragment(MainFragment.mainFragment);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(App.tagName, "PlayerService : onStartCommand");
    MediaInfo mediaInfo = MusicListFragment.getCurrentMp3Info();
    youTubePlayer.loadVideo(mediaInfo.video_id);
    youTubePlayer.play();
    return super.onStartCommand(intent, flags, startId);
}   }

0 个答案:

没有答案