package com.weikou.beibeivideo.ui.video;
|
|
import android.content.BroadcastReceiver;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.IntentFilter;
|
import android.content.pm.ActivityInfo;
|
import android.content.res.Configuration;
|
import android.graphics.Bitmap;
|
import android.media.MediaPlayer;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
import android.os.Bundle;
|
import android.support.annotation.NonNull;
|
import android.support.annotation.Nullable;
|
import android.util.Log;
|
import android.view.LayoutInflater;
|
import android.view.SurfaceHolder;
|
import android.view.SurfaceView;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.ViewGroup;
|
import android.widget.Button;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.androidquery.AQuery;
|
import com.androidquery.callback.AjaxStatus;
|
import com.androidquery.callback.BitmapAjaxCallback;
|
import com.bumptech.glide.Glide;
|
import com.fun.xm.Definition;
|
import com.fun.xm.FSCallback;
|
import com.fun.xm.FSIVideoPlayer;
|
import com.fun.xm.FSPlayer;
|
import com.fun.xm.FSVideoReqData;
|
import com.fun.xm.ad.FSAD;
|
import com.fun.xm.ad.FSSRAdData;
|
import com.fun.xm.ad.FSThirdAd;
|
import com.fun.xm.ad.adloader.FSSRFeedAdLoader;
|
import com.fun.xm.ad.adview.FSFeedAD;
|
import com.fun.xm.ad.callback.FSFeedAdCallBack;
|
import com.fun.xm.ad.listener.FSADMediaListener;
|
import com.funshion.video.logger.FSLogcat;
|
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.lcjian.library.RetainViewFragment;
|
import com.lcjian.library.widget.RatioLayout;
|
import com.qq.e.ads.cfg.VideoOption;
|
import com.qq.e.ads.nativ.MediaView;
|
import com.qq.e.ads.nativ.NativeADEventListener;
|
import com.qq.e.ads.nativ.NativeUnifiedADData;
|
import com.qq.e.comm.constants.AdPatternType;
|
import com.qq.e.comm.util.AdError;
|
import com.umeng.analytics.MobclickAgent;
|
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
|
import com.weikou.beibeivideo.BeibeiVideoAPI;
|
import com.weikou.beibeivideo.BeibeiVideoApplication;
|
import com.weikou.beibeivideo.R;
|
import com.weikou.beibeivideo.entity.PlayUrl;
|
import com.weikou.beibeivideo.entity.VideoDetailInfo;
|
import com.weikou.beibeivideo.entity.VideoInfo;
|
import com.weikou.beibeivideo.entity.VideoResource;
|
import com.weikou.beibeivideo.entity.video.FunshionPlayInfo;
|
import com.weikou.beibeivideo.util.FunshionConstant;
|
import com.weikou.beibeivideo.util.UmengEventUtil;
|
import com.weikou.beibeivideo.util.UserUtil;
|
import com.weikou.beibeivideo.util.VideoUtil;
|
import com.weikou.beibeivideo.util.downutil.StringUtils;
|
import com.weikou.beibeivideo.util.ui.IPageEventListener;
|
import com.yeshi.video.player.common.VideoDefinition;
|
import com.yeshi.video.player.common.VideoEpisode;
|
import com.yeshi.video.player.common.VideoPlayerController;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
|
import de.greenrobot.event.EventBus;
|
|
public class FunshionPlayerFragment extends RetainViewFragment implements OnClickListener, VideoPlayerController.IVideoPlayerListener {
|
private final static String TAG = FunshionPlayerFragment.class.getName();
|
|
//媒体视频播放器
|
private MediaPlayer mVideoPlayer = new MediaPlayer();
|
//视频用 surfaceView
|
private SurfaceView mVideoSurfView;
|
|
//播放逻辑调度
|
private FSIVideoPlayer mFunshionIVideoPlayer;
|
|
//清晰度列表
|
FSVideoReqData mFSVideoReqData;
|
private List<Definition> mDefinitions;
|
private Definition mCurrDefinition;
|
|
private IPageEventListener pageEventListener;
|
|
private VideoInfo mVideoInfo;
|
private int mCurrentPosition;
|
private PlayUrl playUrl;
|
private boolean collected;
|
private VideoResource videoResource;
|
|
private VideoPlayerController vpc_funshion;
|
|
private RatioLayout rl_container;
|
|
private FrameLayout fl_container, fl_ad;
|
|
//风行广告加载器
|
private FSSRFeedAdLoader fssrFeedAdLoader;
|
|
private FunshionPlayInfo playInfo;
|
|
private MediaPlayer mediaPlayer;
|
|
//展示广告
|
private boolean mShowAd = false;
|
|
public void setPageEventListener(IPageEventListener pageEventListener) {
|
this.pageEventListener = pageEventListener;
|
}
|
|
|
@Override
|
public int getContentResource() {
|
return R.layout.fragment_player_funshion;
|
}
|
|
|
private void initPlayer() {
|
mVideoSurfView.getHolder().addCallback(mVideoSurfaceViewCallBack);
|
mVideoPlayer.setOnPreparedListener(mOnPreparedListener);
|
mVideoPlayer.setOnErrorListener(mOnErrorListener);
|
initAccessToken();
|
if (getArguments() != null) {
|
mVideoInfo = (VideoInfo) getArguments().getSerializable("videoInfo");
|
mCurrentPosition = getArguments().getInt("position", 0);
|
playUrl = (PlayUrl) getArguments().getSerializable("playUrl");
|
collected = getArguments().getBoolean("collect", false);
|
videoResource = (VideoResource) getArguments().getSerializable("resource");
|
|
playInfo = new FunshionPlayInfo();
|
playInfo.setCollected(collected);
|
playInfo.setPlayUrl(playUrl);
|
playInfo.setPosition(mCurrentPosition);
|
playInfo.setVideoInfo(mVideoInfo);
|
}
|
|
}
|
|
private void initAd() {
|
FSAD.init(getContext().getApplicationContext(), Collections.singletonList(FunshionConstant.AD_ADP), FunshionConstant.AD_PARTENER, FunshionConstant.AD_TOKEN, FunshionConstant.AD_CHANNEL, FunshionConstant.AD_PRE_DOWNLOAD_CHANNEL);
|
fssrFeedAdLoader = new FSSRFeedAdLoader(getContext(), FunshionConstant.AD_CHANNEL);
|
}
|
|
/**
|
* 加载广告
|
*
|
* @param iadLoadListener
|
*/
|
private synchronized void loadAD(IADLoadListener iadLoadListener) {
|
if (!mShowAd) {
|
iadLoadListener.finishAd();
|
return;
|
}
|
|
fl_ad.setVisibility(View.VISIBLE);
|
String adid = "bfys_a_qt";
|
int width = fl_ad.getWidth();
|
int height = fl_ad.getHeight();
|
Log.i(TAG, String.format("fl_ad: width:%s height:%s", width, height));
|
fssrFeedAdLoader.loadAD(adid, "", width, height, true, false, new FSFeedAdCallBack() {
|
|
@Override
|
public void onClick() {
|
|
}
|
|
@Override
|
public void onAdLoadedFail(int i, String s) {
|
Log.i(TAG, "onAdLoadedFail:" + s);
|
iadLoadListener.finishAd();
|
}
|
|
@Override
|
public void onCreate(FSFeedAD fsADView) {
|
Log.i(TAG, "onCreate");
|
NativeUnifiedADData gdtad = fsADView.getAd();
|
FSSRAdData fsad = fsADView.getFSAd();
|
if (null != gdtad) {
|
//广点通数据自渲染
|
initGDTSelfRenderView(fsADView, gdtad, iadLoadListener);
|
}
|
if (null != fsad) {
|
//风行数据自渲染
|
initFSSelfRenderView(fsADView, fsad, iadLoadListener);
|
}
|
if (fsADView.getParent() != null) {
|
((ViewGroup) fsADView.getParent()).removeAllViews();
|
}
|
fl_ad.removeAllViews();
|
fl_ad.addView(fsADView);
|
fsADView.render();
|
}
|
|
@Override
|
public void onCreateThirdAD(List<FSThirdAd> list) {
|
Log.i(TAG, "onCreateThirdAD");
|
}
|
|
@Override
|
public void onADShow() {
|
Log.i(TAG, "onADShow");
|
}
|
});
|
|
|
}
|
|
private void initFSSelfRenderView(final FSFeedAD fsADView, FSSRAdData fsad, IADLoadListener iadLoadListener) {
|
View view = mLayoutInflater.inflate(R.layout.item_funshion_self_ad_view, null);
|
tv_skip = view.findViewById(R.id.tv_skip);
|
tv_skip.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
iadLoadListener.finishAd();
|
}
|
});
|
TextView tv_detail = view.findViewById(R.id.tv_ad_detail);
|
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, fl_container.getHeight()));
|
if (fsad.isImage()) {
|
ImageView imageView = new ImageView(getContext());
|
Glide.with(this).load(fsad.getMaterial()).into(imageView);
|
List<View> clickableViews = new ArrayList<>();
|
clickableViews.add(imageView);
|
clickableViews.add(tv_detail);
|
fsADView.bindAdToView(imageView, clickableViews, null);
|
} else if (fsad.isVideo()) {
|
MediaView mediaView = new MediaView(getContext());
|
|
fsADView.bindMediaView(mediaView, getVideoOption(), new FSADMediaListener() {
|
@Override
|
public void onVideoClicked() {
|
|
}
|
|
@Override
|
public void onVideoCompleted() {
|
Log.i(TAG, "onVideoCompleted");
|
iadLoadListener.finishAd();
|
}
|
|
@Override
|
public void onVideoError(int i, String s) {
|
iadLoadListener.finishAd();
|
}
|
|
@Override
|
public void onVideoInit() {
|
Log.i(TAG, "onVideoInit");
|
}
|
|
@Override
|
public void onVideoLoaded(int i) {
|
Log.i(TAG, "onVideoLoaded");
|
}
|
|
@Override
|
public void onVideoLoading() {
|
Log.i(TAG, "onVideoLoading");
|
}
|
|
@Override
|
public void onVideoPause() {
|
Log.i(TAG, "onVideoPause");
|
}
|
|
@Override
|
public void onVideoReady() {
|
Log.i(TAG, "onVideoReady");
|
}
|
|
@Override
|
public void onVideoResume() {
|
Log.i(TAG, "onVideoResume");
|
}
|
|
@Override
|
public void onVideoStart() {
|
Log.i(TAG, "onVideoStart");
|
}
|
|
@Override
|
public void onVideoStop() {
|
Log.i(TAG, "onVideoStop");
|
}
|
});
|
List<View> clickableViews = new ArrayList<>();
|
clickableViews.add(mediaView);
|
clickableViews.add(tv_detail);
|
fsADView.setMute(false);
|
fsADView.bindAdToView(view, clickableViews, null);
|
}
|
}
|
|
|
//广点通广告控件
|
protected AQuery mAQuery;
|
protected Button mDownloadButton;
|
protected MediaView mMediaView;
|
protected ImageView mImagePoster;
|
protected LinearLayout mNative3imgAdContainer;
|
private TextView tv_skip;//跳过
|
private LayoutInflater mLayoutInflater;
|
|
private NativeUnifiedADData gdtAd = null;
|
|
int totalTime = 0;
|
|
private Runnable timer = null;
|
|
private synchronized void autoTimer(final View view) {
|
|
if (timer == null) {
|
timer = new Runnable() {
|
@Override
|
public void run() {
|
Log.i(TAG, "autoTimer:" + (remainTime - (gdtAd.getVideoCurrentPosition() / 1000)));
|
if (remainTime - (gdtAd.getVideoCurrentPosition() / 1000) > 0) {
|
tv_skip.setText((remainTime - (gdtAd.getVideoCurrentPosition() / 1000)) + "s后可跳过");
|
tv_skip.setClickable(false);
|
} else {
|
tv_skip.setText("跳过");
|
tv_skip.setClickable(true);
|
}
|
autoTimer(view);
|
}
|
};
|
view.postDelayed(timer, 1000);
|
} else {
|
view.removeCallbacks(timer);
|
view.postDelayed(timer, 1000);
|
}
|
}
|
|
|
int remainTime = 0;
|
|
//广告播放出错
|
Runnable playADErrorRunnable = new Runnable() {
|
@Override
|
public void run() {
|
if (gdtAd != null) {
|
gdtAd.destroy();
|
adShowFinish(playInfo);
|
}
|
}
|
};
|
|
//自渲染view
|
private void initGDTSelfRenderView(FSFeedAD fsADView, NativeUnifiedADData ad, IADLoadListener iadLoadListener) {
|
if (mLayoutInflater == null)
|
return;
|
gdtAd = ad;
|
gdtAd.setNativeAdEventListener(new NativeADEventListener() {
|
@Override
|
public void onADClicked() {
|
Log.v(TAG, "onADClicked");
|
}
|
|
@Override
|
public void onADError(AdError adError) {
|
Log.v(TAG, "onADError");
|
}
|
|
@Override
|
public void onADExposed() {
|
Log.v(TAG, "onADExposed");
|
}
|
|
@Override
|
public void onADStatusChanged() {
|
Log.v(TAG, "onADStatusChanged");
|
}
|
});
|
|
final View mContainer = mLayoutInflater.inflate(R.layout.item_funshion_gdt_ad_view, null);
|
mDownloadButton = mContainer.findViewById(R.id.btn_download);
|
mMediaView = mContainer.findViewById(R.id.gdt_media_view);
|
mImagePoster = mContainer.findViewById(R.id.img_poster);
|
mNative3imgAdContainer = mContainer.findViewById(R.id.native_3img_ad_container);
|
tv_skip = mContainer.findViewById(R.id.tv_skip);
|
tv_skip.setVisibility(View.GONE);
|
tv_skip.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
iadLoadListener.finishAd();
|
}
|
});
|
TextView tv_ad_detail = mContainer.findViewById(R.id.tv_ad_detail);
|
|
mAQuery = new AQuery(mContainer);
|
int patternType = ad.getAdPatternType();
|
if (patternType == AdPatternType.NATIVE_VIDEO) {
|
ViewGroup.LayoutParams params = mContainer.getLayoutParams();
|
if (null == params) {
|
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
} else {
|
params.height = FrameLayout.LayoutParams.MATCH_PARENT;
|
}
|
mContainer.setLayoutParams(params);
|
}
|
if (patternType == AdPatternType.NATIVE_2IMAGE_2TEXT
|
|| patternType == AdPatternType.NATIVE_VIDEO) {
|
mAQuery.id(R.id.img_poster).visibility(View.VISIBLE);
|
mAQuery.id(R.id.img_logo).image(ad.getIconUrl(), false, true);
|
Log.v(TAG, "Imgurl:" + ad.getImgUrl());
|
mAQuery.id(R.id.img_poster).image(ad.getImgUrl(), false, true, 0, 0,
|
new BitmapAjaxCallback() {
|
@Override
|
protected void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status) {
|
if (iv.getVisibility() == View.VISIBLE) {
|
iv.setImageBitmap(bm);
|
}
|
}
|
});
|
mAQuery.id(R.id.text_title).textColor(R.color.colorAccent);
|
mAQuery.id(R.id.text_title).text(ad.getTitle());
|
mAQuery.id(R.id.text_desc).text(ad.getDesc());
|
} else if (patternType == AdPatternType.NATIVE_3IMAGE) {
|
mAQuery.id(R.id.img_1).image(ad.getImgList().get(0), false, true);
|
mAQuery.id(R.id.img_2).image(ad.getImgList().get(1), false, true);
|
mAQuery.id(R.id.img_3).image(ad.getImgList().get(2), false, true);
|
mAQuery.id(R.id.native_3img_title).text(ad.getTitle());
|
mAQuery.id(R.id.native_3img_desc).text(ad.getDesc());
|
} else if (patternType == AdPatternType.NATIVE_1IMAGE_2TEXT) {
|
mAQuery.id(R.id.img_logo).image(ad.getImgUrl(), false, true);
|
mAQuery.id(R.id.img_poster).clear();
|
mAQuery.id(R.id.text_title).text(ad.getTitle());
|
mAQuery.id(R.id.text_desc).text(ad.getDesc());
|
}
|
|
List<View> clickableViews = new ArrayList<>();
|
List<View> customClickableViews = new ArrayList<>();
|
// 所有广告类型,注册mDownloadButton的点击事件
|
clickableViews.add(tv_ad_detail);
|
if (ad.getAdPatternType() == AdPatternType.NATIVE_2IMAGE_2TEXT ||
|
ad.getAdPatternType() == AdPatternType.NATIVE_1IMAGE_2TEXT) {
|
// 双图双文、单图双文:注册mImagePoster的点击事件
|
clickableViews.add(mImagePoster);
|
} else if (ad.getAdPatternType() != AdPatternType.NATIVE_VIDEO) {
|
// 三小图广告:注册native_3img_ad_container的点击事件
|
clickableViews.add(mNative3imgAdContainer);
|
}
|
//作为customClickableViews传入,点击不进入详情页,直接下载或进入落地页,只有视频广告生效,
|
// 如果是图文,clickableViews和customClickableViews点击逻辑一致
|
|
fsADView.bindAdToView(mContainer, clickableViews, mDownloadButton);
|
|
if (ad.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
|
// 视频广告,注册mMediaView的点击事件
|
fsADView.post(new Runnable() {
|
@Override
|
public void run() {
|
Log.v(TAG, "showAd type video 2");
|
mImagePoster.setVisibility(View.GONE);
|
mMediaView.setVisibility(View.VISIBLE);
|
}
|
});
|
|
//4秒内视频还没加载完成的话就跳过
|
tv_skip.removeCallbacks(playADErrorRunnable);
|
tv_skip.postDelayed(playADErrorRunnable, 10000);
|
fsADView.bindMediaView(mMediaView, getVideoOption(), new FSADMediaListener() {
|
@Override
|
public void onVideoInit() {
|
Log.d(TAG, "onVideoInit: ");
|
}
|
|
@Override
|
public void onVideoLoading() {
|
Log.d(TAG, "onVideoLoading: ");
|
}
|
|
@Override
|
public void onVideoReady() {
|
Log.d(TAG, "onVideoReady");
|
}
|
|
@Override
|
public void onVideoLoaded(int videoDuration) {
|
Log.d(TAG, "onVideoLoaded: ");
|
tv_skip.removeCallbacks(playADErrorRunnable);
|
}
|
|
@Override
|
public void onVideoStart() {
|
Log.d(TAG, "onVideoStart");
|
Log.d(TAG, "onVideoStart");
|
if (ad.getVideoDuration() / 1000 > 5)
|
remainTime = 5;
|
else
|
remainTime = ad.getVideoDuration() / 1000;
|
|
autoTimer(mContainer);
|
mContainer.post(new Runnable() {
|
@Override
|
public void run() {
|
tv_skip.setVisibility(View.VISIBLE);
|
}
|
});
|
}
|
|
@Override
|
public void onVideoStop() {
|
Log.d(TAG, "onVideoStop");
|
}
|
|
@Override
|
public void onVideoPause() {
|
Log.d(TAG, "onVideoPause: ");
|
}
|
|
@Override
|
public void onVideoResume() {
|
Log.d(TAG, "onVideoResume: ");
|
}
|
|
@Override
|
public void onVideoClicked() {
|
Log.d(TAG, "onVideoClicked");
|
}
|
|
@Override
|
public void onVideoCompleted() {
|
Log.d(TAG, "onVideoCompleted: ");
|
iadLoadListener.finishAd();
|
}
|
|
@Override
|
public void onVideoError(int i, String s) {
|
iadLoadListener.finishAd();
|
}
|
});
|
// mMediaView.post(new Runnable() {
|
// @Override
|
// public void run() {
|
// ad.startVideo();
|
// }
|
// });
|
|
|
} else if (ad.getAdPatternType() == AdPatternType.NATIVE_2IMAGE_2TEXT ||
|
ad.getAdPatternType() == AdPatternType.NATIVE_1IMAGE_2TEXT) {
|
// 双图双文、单图双文:注册mImagePoster的点击事件
|
clickableViews.add(mImagePoster);
|
} else {
|
// 三小图广告:注册native_3img_ad_container的点击事件
|
clickableViews.add(mNative3imgAdContainer);
|
}
|
}
|
|
private VideoOption getVideoOption() {
|
VideoOption.Builder builder = new VideoOption.Builder();
|
builder.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.ALWAYS);
|
builder.setAutoPlayMuted(false);
|
builder.setDetailPageMuted(false);
|
builder.setNeedCoverImage(true);
|
builder.setNeedProgressBar(true);
|
builder.setEnableDetailPage(true);
|
builder.setEnableUserControl(false);
|
VideoOption videoOption = builder.build();
|
return videoOption;
|
}
|
|
|
//广告展示完成
|
private void adShowFinish(FunshionPlayInfo info) {
|
fl_ad.setVisibility(View.GONE);
|
fl_ad.removeAllViews();
|
//播放视频
|
mVideoSurfView.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
//播放视频
|
playVideo(info.getPlayUrl().getCode());
|
}
|
}, 1000);
|
}
|
|
private void initView(View view) {
|
mVideoSurfView = view.findViewById(R.id.video_view);
|
vpc_funshion = view.findViewById(R.id.vpc_funshion);
|
rl_container = view.findViewById(R.id.rl_container);
|
fl_container = view.findViewById(R.id.fl_container);
|
fl_ad = view.findViewById(R.id.fl_ad);
|
mLayoutInflater = LayoutInflater.from(getContext());
|
}
|
|
//创建广播接受者对象
|
BatteryReceiver batteryReceiver = new BatteryReceiver();
|
NetworkReceiver networkReceiver = new NetworkReceiver();
|
|
@Override
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
//注册receiver
|
}
|
|
@Override
|
public void onCreateView(View contentView, Bundle savedInstanceState) {
|
initView(contentView);
|
initPlayer();
|
getContext().registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
getContext().registerReceiver(networkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
|
initAd();
|
mShowAd = true;
|
onEventMainThread(playInfo);
|
}
|
|
@Override
|
public void onDestroyView() {
|
super.onDestroyView();
|
getContext().unregisterReceiver(batteryReceiver);
|
getContext().unregisterReceiver(networkReceiver);
|
}
|
|
@Override
|
public void onClick(View v) {
|
|
}
|
|
@Override
|
public void onConfigurationChanged(Configuration newConfig) {
|
super.onConfigurationChanged(newConfig);
|
}
|
|
/**
|
* 播放视频
|
*/
|
public synchronized void playVideo(String code) {
|
createPlayer();
|
if (mVideoPlayer.isPlaying()) {
|
FSLogcat.e(TAG, "Call player to stop");
|
mVideoPlayer.stop();
|
mVideoPlayer.reset();
|
}
|
//!!!注意播放长短视频构建的FSVideoReqData有区别。短视频没有epso分集ID。长视频有。填写错误将无法正常播放
|
//FSVideoReqData构建需要的数据需要从服务器获取。主要是媒体资源videoID:“VIDEO_ID” 默认 清晰度:new Definition(Definition.CLARITY_LOW) 以及Accesstoken:Accesstoken
|
mFSVideoReqData = new FSVideoReqData(code, new Definition(Definition.CLARITY_SUPPER));
|
try {
|
Log.d(TAG, "request");
|
mFunshionIVideoPlayer.requestAndPrepare(mFSVideoReqData);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
public synchronized void stopPlay() {
|
if (mVideoPlayer != null && mVideoPlayer.isPlaying()) {
|
FSLogcat.e(TAG, "Call player to stop");
|
mVideoPlayer.stop();
|
mVideoPlayer.reset();
|
}
|
}
|
|
|
private void createPlayer() {
|
if (mFunshionIVideoPlayer != null) {
|
return;
|
}
|
try {
|
//APICODE 为风行派申请的APICODE值
|
mFunshionIVideoPlayer = FSPlayer.create(getContext(), mFSCall);
|
//可以打开调试信息方便查询问题
|
mFunshionIVideoPlayer.setDebug(true);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
private String getDefinitionName(String clarity) {
|
switch (clarity) {
|
case "tv":
|
case "dvd":
|
return "标清";
|
case "hd":
|
return "高清";
|
case "sdvd":
|
return "超清";
|
default:
|
return "标清";
|
}
|
|
|
}
|
|
private String videoPlayUrl = "";
|
|
|
FSCallback mFSCall = new FSCallback() {
|
|
@Override
|
public void onFailed(com.funshion.playsdk.constant.FSError fsError) {
|
Log.d(TAG, fsError.errorMessage);
|
}
|
|
@Override
|
public void onDefinition(List<Definition> definitions, Definition currDefinition) {
|
for (Definition definition : definitions) {
|
Log.e("FE", "get Definition " + definition);
|
}
|
FunshionPlayerFragment.this.mDefinitions = definitions;
|
FunshionPlayerFragment.this.mCurrDefinition = currDefinition;
|
|
//设置清晰度
|
List<VideoDefinition> vdfList = new ArrayList<>();
|
if (definitions != null)
|
for (Definition df : definitions) {
|
VideoDefinition vdf = new VideoDefinition();
|
vdf.setClarity(df.getStringDefinition());
|
vdf.setName(getDefinitionName(df.getStringDefinition()));
|
vdfList.add(vdf);
|
}
|
VideoDefinition vdf = new VideoDefinition();
|
vdf.setClarity(mCurrDefinition.getStringDefinition());
|
vdf.setName(getDefinitionName(mCurrDefinition.getStringDefinition()));
|
vpc_funshion.setDefinition(vdfList, vdf);
|
}
|
|
public void onReceiveUrl(String url) {
|
Log.d(TAG, "onReceiveUrl" + url);
|
try {
|
videoPlayUrl = url;
|
mVideoPlayer.reset();
|
mVideoPlayer.setDataSource(url);
|
mVideoPlayer.prepareAsync();
|
} catch (IllegalArgumentException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
} catch (SecurityException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
} catch (IllegalStateException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
};
|
|
boolean isReady = false;
|
|
int position = 0;
|
|
SurfaceHolder.Callback mVideoSurfaceViewCallBack = new SurfaceHolder.Callback() {
|
|
@Override
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
isReady = false;
|
|
Log.d(TAG, "surfaceDestroyed");
|
if (mVideoPlayer != null) {
|
position = mVideoPlayer.getCurrentPosition();
|
Log.d(TAG, "当前播放时间:" + position);
|
}
|
if (mVideoPlayer.isPlaying()) {
|
mVideoPlayer.stop();
|
}
|
}
|
|
@Override
|
public void surfaceCreated(SurfaceHolder holder) {
|
Log.d(TAG, "surfaceCreated");
|
isReady = true;
|
mVideoPlayer.setDisplay(holder);
|
if (!"".equals(videoPlayUrl) && !mVideoPlayer.isPlaying()) {
|
try {
|
mVideoPlayer.reset();
|
mVideoPlayer.setDataSource(videoPlayUrl);
|
mVideoPlayer.prepare();
|
if (position > 0)
|
mVideoPlayer.seekTo(position);
|
Log.d(TAG, "续播时间:" + position);
|
position = -1;
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
@Override
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
|
Log.d(TAG, "surfaceChanged");
|
}
|
};
|
|
|
MediaPlayer.OnPreparedListener mOnPreparedListener = new MediaPlayer.OnPreparedListener() {
|
|
@Override
|
public void onPrepared(MediaPlayer mp) {
|
Log.i(TAG, "onPrepared");
|
mediaPlayer = mp;
|
mFunshionIVideoPlayer.onMediaPlayerPrepared();
|
autoReSize(mp);
|
vpc_funshion.onPrepared(mp);
|
if (position > 0)
|
mVideoPlayer.seekTo(position);
|
}
|
};
|
|
private void autoReSize(MediaPlayer mp) {
|
Log.i(TAG, "autoReSize");
|
final int vWidth = mp.getVideoWidth();
|
final int vHeight = mp.getVideoHeight();
|
if (vHeight == 0 || vWidth == 0)
|
return;
|
//获取容器的宽
|
fl_container.post(new Runnable() {
|
@Override
|
public void run() {
|
float ratio = new BigDecimal(vHeight).divide(new BigDecimal(vWidth), 4, RoundingMode.FLOOR).floatValue();
|
int cwidth = fl_container.getWidth();
|
int cheight = fl_container.getHeight();
|
Log.i(TAG, String.format("MediaPlayer: width-%s height-%s", vWidth, vHeight));
|
|
ViewGroup.LayoutParams params = rl_container.getLayoutParams();
|
if (vHeight * cwidth > vWidth * cheight) {
|
Log.i(TAG, "宽自适应");
|
params.height = fl_container.getHeight();
|
params.width = (int) (params.height / ratio);
|
} else {
|
Log.i(TAG, "高自适应");
|
params.width = fl_container.getWidth();
|
params.height = (int) (params.width * ratio);
|
}
|
rl_container.setLayoutParams(params);
|
|
rl_container.setRatio(ratio);
|
Log.i(TAG, String.format("Ratio: %s", ratio));
|
|
mVideoSurfView.post(new Runnable() {
|
@Override
|
public void run() {
|
Log.i(TAG, String.format("mVideoSurfView: width-%s height-%s", mVideoSurfView.getWidth(), mVideoSurfView.getHeight()));
|
|
|
}
|
});
|
}
|
});
|
}
|
|
MediaPlayer.OnErrorListener mOnErrorListener = new MediaPlayer.OnErrorListener() {
|
|
@Override
|
public boolean onError(MediaPlayer mp, int what, int extra) {
|
if (mFunshionIVideoPlayer != null)
|
mFunshionIVideoPlayer.onMediaPlayerError(what, extra);
|
return false;
|
}
|
};
|
|
//TODO 获取AcessToken
|
private void initAccessToken() {
|
|
}
|
|
@Override
|
public void onDestroy() {
|
stopPlay();
|
if (tv_skip != null)
|
tv_skip.setVisibility(View.GONE);
|
if (mFunshionIVideoPlayer != null) {
|
mFunshionIVideoPlayer.onActivityDestroy();
|
mFunshionIVideoPlayer.release();
|
}
|
mLayoutInflater = null;
|
super.onDestroy();
|
}
|
|
@Override
|
public void onDetach() {
|
super.onDetach();
|
}
|
|
@Override
|
public void onStop() {
|
super.onStop();
|
if (mVideoPlayer != null && mVideoPlayer.isPlaying())
|
mVideoPlayer.pause();
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
if (mFunshionIVideoPlayer != null)
|
mFunshionIVideoPlayer.onActivityResume();
|
EventBus.getDefault().register(this);
|
if (gdtAd != null) {
|
gdtAd.resume();
|
}
|
|
vpc_funshion.onResume();
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
if (mVideoPlayer.isPlaying())
|
mVideoPlayer.pause();
|
if (mFunshionIVideoPlayer != null)
|
mFunshionIVideoPlayer.onActivityPause();
|
EventBus.getDefault().unregister(this);
|
vpc_funshion.onPause();
|
}
|
|
|
public void requestLayout() {
|
Log.i(TAG, "requestLayout");
|
if (mediaPlayer != null)
|
autoReSize(mediaPlayer);
|
else
|
autoReSize(mVideoPlayer);
|
if (getActivity() != null)
|
vpc_funshion.changeOrien(getActivity().getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
}
|
|
//电池电量监控
|
class BatteryReceiver extends BroadcastReceiver {
|
|
@Override
|
public void onReceive(Context context, Intent intent) {
|
// TODO Auto-generated method stub
|
//判断它是否是为电量变化的Broadcast Action
|
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {//电量变化
|
//获取当前电量
|
int level = intent.getIntExtra("level", 0);
|
//电量的总刻度
|
int scale = intent.getIntExtra("scale", 100);
|
//把它转成百分比
|
if (vpc_funshion != null)
|
vpc_funshion.setBattery((level * 100) / scale);
|
}
|
}
|
}
|
|
public class NetworkReceiver extends BroadcastReceiver {
|
public void onReceive(Context context, Intent intent) {
|
String action = intent.getAction();
|
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
|
vpc_funshion.setNetworkInfo(networkInfo, false);
|
}
|
}
|
|
}
|
|
|
public void onEventMainThread(FunshionPlayInfo info) {
|
if (VideoUtil.videoEpisodeList == null || VideoUtil.videoEpisodeList.size() == 0) {
|
//加载缓存内容
|
VideoUtil.loadVideoEpisodeListCache(BeibeiVideoApplication.application);
|
}
|
//风行视频播放
|
UmengEventUtil.funtvPlay(getContext());
|
|
Log.i(TAG, "onEventMainThread:FunshionPlayInfo");
|
mCurrentPosition = info.getPosition();
|
List<VideoEpisode> episodes = new ArrayList<>();
|
for (int i = 0; i < VideoUtil.videoEpisodeList.size(); i++) {
|
VideoDetailInfo detail = VideoUtil.videoEpisodeList.get(i);
|
VideoEpisode es = new VideoEpisode();
|
es.setId(detail.getId());
|
es.setTag(detail.getTag());
|
es.setName(detail.getName());
|
es.setVideoId(info.getVideoInfo().getId());
|
if (mCurrentPosition == i)
|
es.setVideoUrl(info.getPlayUrl().getUrl());
|
episodes.add(es);
|
}
|
|
try {
|
vpc_funshion.setContentView(fl_container).setVideoInfo(episodes, info.getPosition(), mVideoInfo.getShowType(), "风行").setVideoPlayerListener(this).build(getActivity(), FunshionPlayerFragment.this);
|
loadAD(new IADLoadListener() {
|
@Override
|
public void finishAd() {
|
vpc_funshion.post(new Runnable() {
|
@Override
|
public void run() {
|
tv_skip.setVisibility(View.GONE);
|
adShowFinish(info);
|
}
|
});
|
}
|
});
|
} catch (Exception e) {
|
Log.i(TAG, "出错了:" + e.getMessage());
|
}
|
|
}
|
|
//监听
|
@Override
|
public void onNext() {
|
if (mCurrentPosition >= VideoUtil.videoEpisodeList.size() - 1) {
|
Toast.makeText(getContext(), "没有下一集了", Toast.LENGTH_SHORT).show();
|
return;
|
}
|
|
selectEpisode(mCurrentPosition + 1, null);
|
}
|
|
@Override
|
public void onBack() {//返回
|
if (getActivity().getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
|
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
} else {//结束
|
getActivity().finish();
|
}
|
}
|
|
@Override
|
public void onVideoPause() {
|
|
}
|
|
@Override
|
public void onVideoPlay() {
|
|
}
|
|
@Override
|
public void changeDefinition(int p, VideoDefinition definition) {
|
Toast.makeText(BeibeiVideoApplication.application, "正在切换清晰度", Toast.LENGTH_SHORT).show();
|
if (mVideoPlayer != null && mVideoPlayer.isPlaying())
|
position = mVideoPlayer.getCurrentPosition();
|
mFunshionIVideoPlayer.changeDefinition(mFSVideoReqData.getUniqueID(), mDefinitions.get(p));
|
}
|
|
@Override
|
public void selectEpisode(int p, VideoEpisode episode) {
|
mShowAd = false;
|
//选集
|
playInfo = new FunshionPlayInfo();
|
playInfo.setVideoInfo(mVideoInfo);
|
playInfo.setPosition(p);
|
playInfo.setCollected(collected);
|
VideoDetailInfo detailInfo = VideoUtil.videoEpisodeList.get(p);
|
String uid = UserUtil.getUid(BeibeiVideoApplication.application);
|
if (mVideoPlayer != null && mVideoPlayer.isPlaying())
|
mVideoPlayer.pause();
|
|
//播放统计
|
BeibeiVideoAPI.playStatistic(BeibeiVideoApplication.application, mVideoInfo.getId(), videoResource.getId() + "", new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
|
}
|
});
|
|
BeibeiVideoAPI.getPlayUrl(BeibeiVideoApplication.application, uid, detailInfo.getType(), mVideoInfo.getId(),
|
detailInfo.getId(), videoResource.getId(), detailInfo.geteId(),
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE)
|
.create();
|
VideoResource resource = gson.fromJson(
|
jsonObject.getJSONObject("Data")
|
.getJSONObject("Resource")
|
.toString(),
|
new TypeToken<VideoResource>() {
|
}.getType());
|
PlayUrl info = new PlayUrl();
|
info.setResource(resource);
|
JSONObject data = jsonObject.getJSONObject("Data");
|
info.setPlayType(Integer.parseInt(data
|
.optString("PlayType")));
|
info.setUrl(
|
data.optString("Url"));
|
info.setParams(data
|
.optString("Params"));
|
info.setAid(data.optString("Aid"));
|
info.setVid(data.optString("Vid"));
|
info.setCode(jsonObject.getJSONObject("Data")
|
.optString("Code"));
|
playInfo.setPlayUrl(info);
|
mShowAd = false;
|
EventBus.getDefault().post(playInfo);
|
}
|
}
|
});
|
}
|
|
@Override
|
public void collect(boolean collect) {
|
|
}
|
|
|
//保存数据
|
@Override
|
public void onSaveInstanceState(@NonNull Bundle outState) {
|
super.onSaveInstanceState(outState);
|
Log.i(TAG, "onSaveInstanceState");
|
}
|
|
//恢复数据
|
@Override
|
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
|
super.onViewStateRestored(savedInstanceState);
|
Log.i(TAG, "onViewStateRestored");
|
}
|
|
interface IADLoadListener {
|
public void finishAd();
|
}
|
}
|