环境要求:
1.安装youtube apk。 2.网络翻墙。
在没有谷歌登录需求的时候可以不集成谷歌服务框架。当然,这样也会导致youtube应用无法正常使用,会一直弹窗提示没有谷歌服务。由于是定制Rom,我只需把youtube桌面图标隐藏就可以了,有需要的话再预装一个不依赖谷歌服框架的youtube相关应用即可(比如smarttube),内容和正版youtube一样的。
步骤:
1.在youtube开发者平台注册应用,获取API key 值
2.下载youtube sdk jar包
youtube 视频列表接口https://developers.google.cn/youtube/v3/docs/videos/list#http-request
可参考https://blog.csdn.net/u011503666/article/details/107942793https://blog.csdn.net/u011503666/article/details/107942793
YoutubePlayer android sdkhttps://developers.google.com/youtube/android/player3.简单应用场景(手机、电视等)根据视频id(如:fY7jIJE0c7Y),调用youtubePlayer.cueVideo(id)即可。这里不详细介绍了,网上很多样例。
我这里主要介绍一下再副屛(Presentation)中播放的场景。由于YouTubePlayerView必须依赖于一个继承自YouTubeBaseActivity的Activity来进行实例化,否则会报错,源码如下:
而副屛类Presentation本质上是一个Dialog,无法直接实例化?YouTubePlayerView,不能像在MainActivity实现一样直接可以在xml文件中声明就可以了。所以只能曲线救国,大体的思路就是MainActivity继承YouTubeBaseActivity,然后将MainActivity实例作为参数传入Presentation,在Presentation中通过new YouTubePlayerView(activity)来实例化播放组件,然后通过addView动态添加到副屛视图上。主要代码如下:
package com.example.youtube;
import android.app.Activity;
import android.app.Presentation;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;
public class YoutubePlayFragment {
private YouTubePlayerView mPlayerView;
private YouTubePlayer mYouTubePlayer;
private FrameLayout mPlayerViewParent;
private MyPlayerStateChangeListener playerStateChangeListener; // 播放状态监听
private MyPlaybackEventListener playbackEventListener; // 播放事件回调
private YoutubePresentation youtubePresentation;
private Activity mAtv;
private View mRootView;
private Handler mHandler;
private static YoutubePlayFragment instance;
private OnYoutubePlayerInitFinishListener mlInitFinishListener;
public static YoutubePlayFragment getInstance(Activity paramActivity){
if(instance==null){
synchronized (YoutubePlayFragment.class) {
if(instance==null){
instance = new YoutubePlayFragment(paramActivity);
}else{
return instance;
}
}
}
return instance;
}
private void initView(View view){
mHandler = new Handler();
mRootView = view;
this.mPlayerViewParent = (FrameLayout)view.findViewById(R.id.root);
if (this.mPlayerView == null) {
this.mPlayerView = new YouTubePlayerView(getActivity());
}
if(this.mPlayerView.getParent() != null)
((ViewGroup)this.mPlayerView.getParent()).removeView(this.mPlayerView);
this.mPlayerViewParent.addView(this.mPlayerView, new ViewGroup.LayoutParams(-1,-1));
playerStateChangeListener = new MyPlayerStateChangeListener();
playbackEventListener = new MyPlaybackEventListener();
mPlayerView.initialize("你的APP KEY", new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player,boolean arg2) {
// TODO Auto-generated method stub
mYouTubePlayer = player;
mYouTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
mYouTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
mYouTubePlayer.setPlaybackEventListener(playbackEventListener);
mYouTubePlayer.setManageAudioFocus(true);
mlInitFinishListener.onPlayerInited();
}
@Override
public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
}
public void playVideo(String videoId){
if(mYouTubePlayer!=null){
mYouTubePlayer.cueVideo(videoId);
mYouTubePlayer.play();
}
}
public void show()
{
Display[] displays = ((DisplayManager)this.mAtv.getSystemService("display")).getDisplays();
if (this.youtubePresentation == null)
{
this.youtubePresentation = new YoutubePresentation(this.mAtv, (Context)this.mAtv,displays[displays.length - 1]);
}
if (!this.youtubePresentation.isShowing())
{
this.youtubePresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
this.youtubePresentation.show();
}
}
private YoutubePlayFragment(Activity paramActivity)
{
this.mAtv = paramActivity;
}
private Activity getActivity()
{
return this.mAtv;
}
private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener {
@Override
public void onPlaying() {
// Called when playback starts, either due to user action or call to
// play().
Log.i("jlink", "YouTube Playing");
}
@Override
public void onPaused() {
// Called when playback is paused, either due to user action or call
// to pause().
Log.i("jlink", "YouTube Paused");
}
@Override
public void onStopped() {
// Called when playback stops for a reason other than being paused.
Log.i("jlink", "YouTube Stopped");
}
@Override
public void onBuffering(boolean b) {
// Called when buffering starts or ends.
Log.i("jlink", "===youTubePlayer onBuffering");
}
@Override
public void onSeekTo(int i) {
Log.i("jlink", "===youTubePlayer onSeekTo");
// Called when a jump in playback position occurs, either
// due to user scrubbing or call to seekRelativeMillis() or
// seekToMillis()
}
}
private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
@Override
public void onLoading() {
Log.i("jlink", "===youTubePlayer onLoading");
// Called when the player is loading a video
// At this point, it's not ready to accept commands affecting
// playback such as play() or pause()
}
@Override
public void onLoaded(String s) {
Log.i("jlink", "===youTubePlayer onLoaded");
// Called when a video is done loading.
// Playback methods such as play(), pause() or seekToMillis(int) may
// be called after this callback.
mYouTubePlayer.play();
}
@Override
public void onAdStarted() {
// Called when playback of an advertisement starts.
Log.i("jlink", "===youTubePlayer onAdStarted");
}
@Override
public void onVideoStarted() {
// Called when playback of the video starts.
Log.i("jlink", "===youTubePlayer onVideoStarted");
}
@Override
public void onVideoEnded() {
// Called when the video reaches its end.
Log.i("jlink", "===youTubePlayer onVideoEnded");
mYouTubePlayer.seekToMillis(0);
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
// Called when an error occurs.
Log.i("jlink", "===youTubePlayer errorReason==" + errorReason);
}
}
private class YoutubePresentation extends Presentation
{
private View mContentView;
public YoutubePresentation(Activity paramActivity, Context paramContext, Display paramDisplay)
{
super(paramActivity,paramDisplay);
}
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
this.mContentView = View.inflate(getContext(),R.layout.youtube, null);
setContentView(this.mContentView);
YoutubePlayFragment.this.initView(this.mContentView);
}
}
public interface OnYoutubePlayerInitFinishListener{
void onPlayerInited();
}
public void setOnPlayerInitedListener(OnYoutubePlayerInitFinishListener listener){
mlInitFinishListener = listener;
}
}
?使用时直接调用即可:
YoutubePlayFragment.getInstance(MainActivity.this).playVideo(videoId);
|