package com.weikou.beibeivideo.ui.media;
|
|
import android.app.Activity;
|
import android.app.Dialog;
|
import android.content.BroadcastReceiver;
|
import android.content.ContentValues;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.IntentFilter;
|
import android.content.SharedPreferences;
|
import android.content.pm.ActivityInfo;
|
import android.content.res.Configuration;
|
import android.database.Cursor;
|
import android.media.AudioManager;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.support.v4.app.Fragment;
|
import android.support.v4.media.TransportMediator;
|
import android.text.TextUtils;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.ViewGroup;
|
import android.widget.ProgressBar;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.lcjian.library.content.ConnectivityChangeHelper;
|
import com.lcjian.library.content.ConnectivityChangeHelper.OnConnectivityChangeListener;
|
import com.lcjian.library.util.SingleToast;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.lcjian.library.videocomponents.GestureMediaController;
|
import com.lcjian.library.videocomponents.GestureMediaController.GestureTransportController;
|
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
|
import com.weikou.beibeivideo.BeibeiVideoAPI;
|
import com.weikou.beibeivideo.db.WatchHistoryTable;
|
import com.weikou.beibeivideo.entity.PlayUrl;
|
import com.weikou.beibeivideo.entity.VideoDetailInfo;
|
import com.weikou.beibeivideo.entity.VideoInfo;
|
import com.weikou.beibeivideo.ui.common.ShareDialogFragment;
|
import com.weikou.beibeivideo.ui.common.ShareVideoDialogFragment;
|
import com.weikou.beibeivideo.ui.discover.GoodsDetailActivity;
|
import com.weikou.beibeivideo.ui.login.LoginActivity;
|
import com.weikou.beibeivideo.ui.media.VideoDetailActivity.ChangeVideoEvent;
|
import com.weikou.beibeivideo.util.BeibeiConstant;
|
import com.weikou.beibeivideo.util.DownloadUtils;
|
import com.weikou.beibeivideo.util.GetRealPathTask;
|
import com.yeshi.buwanshequ.R;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
|
import de.greenrobot.event.EventBus;
|
|
public class VideoPlayerFragment extends Fragment implements
|
BeibeiMediaController.MediaControllerCallback {
|
|
private VideoInfo mVideoInfo;
|
private int mPlayingPosition;
|
|
// private BeibeiVideoView mVideoView;
|
private ProgressBar pb;
|
private TextView downloadRateView;
|
private TextView loadRateView;
|
private TextView load_tip;
|
private TextView brightness;
|
private TextView volumeness;
|
private TextView progressness;
|
|
private TransportMediator mTransportMediator;
|
private BeibeiMediaController mMediaController;
|
public static BeibeiMediaController mediaController;
|
private GestureMediaController mGestureMediaController;
|
|
private int playFailedtime = 0;
|
|
@Override
|
public void onDefinition() {
|
|
}
|
|
@Override
|
public void onNext() {
|
|
}
|
|
@Override
|
public void onSeek() {
|
|
}
|
|
// private TransportPerformer mTransportPerformer = new TransportPerformer()
|
// {
|
// @Override
|
// public void onStart() {
|
// mVideoView.start();
|
// if (BeibeiVideoApplication.showAd) {
|
// // XfvideoInterstitialManager.shareInstance()
|
// // .defaultInterstitialCancel();
|
// // showInterstitialAd(true);//广点通插屏
|
// }
|
// }
|
//
|
// @Override
|
// public void onStop() {
|
// mVideoView.pause();
|
// }
|
//
|
// @Override
|
// public void onPause() {
|
// mVideoView.pause();
|
// if (BeibeiVideoApplication.showAd) {
|
// XfvideoInterstitialManager.shareInstance()
|
// .defaultInterstitial().interstitialShow(true);
|
// // showInterstitialAd();// 聚效插屏
|
// // showInterstitialAd(true);// 广点通插屏广告
|
// // BtVideoPlayerFragment.initInterstitialAd(getActivity());//百通插屏广告
|
// }
|
// }
|
//
|
// @Override
|
// public long onGetDuration() {
|
// return mVideoView.getDuration();
|
// }
|
//
|
// @Override
|
// public long onGetCurrentPosition() {
|
// return mVideoView.getCurrentPosition();
|
// }
|
//
|
// @Override
|
// public void onSeekTo(long pos) {
|
// mVideoView.seekTo((int) pos);
|
// }
|
//
|
// @Override
|
// public boolean onIsPlaying() {
|
// return mVideoView.isPlaying();
|
// }
|
//
|
// @Override
|
// public int onGetBufferPercentage() {
|
// return mVideoView.getBufferPercentage();
|
// }
|
//
|
// @Override
|
// public int onGetTransportControlFlags() {
|
// int flags = TransportMediator.FLAG_KEY_MEDIA_PLAY
|
// | TransportMediator.FLAG_KEY_MEDIA_PLAY_PAUSE
|
// | TransportMediator.FLAG_KEY_MEDIA_STOP
|
// | TransportMediator.FLAG_KEY_MEDIA_PAUSE;
|
// return flags;
|
// }
|
// };
|
|
private GestureTransportController mGestureTransportController = new GestureMediaController.SimpleGestureTransportController() {
|
|
@Override
|
public Activity getActivityInstance() {
|
return getActivity();
|
}
|
|
@Override
|
public long getDuration() {
|
return mTransportMediator.getDuration();
|
}
|
|
@Override
|
public long getCurrentPosition() {
|
return mTransportMediator.getCurrentPosition();
|
}
|
|
@Override
|
public void toggleController() {
|
if (mMediaController.isShowing()) {
|
mMediaController.hide();
|
} else {
|
mMediaController.show();
|
}
|
}
|
|
@Override
|
public void togglePausePlay() {
|
if (mTransportMediator.isPlaying()) {
|
mTransportMediator.pausePlaying();
|
} else {
|
mTransportMediator.startPlaying();
|
}
|
}
|
|
@Override
|
public void showAdjustVolume(int requestVolumeness, int maxVolume) {
|
if (requestVolumeness == 0) {
|
volumeness.setCompoundDrawablesWithIntrinsicBounds(0,
|
R.drawable.ic_media_volume_mute, 0, 0);
|
} else {
|
volumeness.setCompoundDrawablesWithIntrinsicBounds(0,
|
R.drawable.ic_media_volume, 0, 0);
|
}
|
mMediaController.setVolumeness(requestVolumeness);
|
volumeness.setText("音量:"
|
+ String.format("%1$1.0f%%", requestVolumeness * 100f
|
/ maxVolume));
|
setVolumenessVisibility(true);
|
}
|
|
@Override
|
public void showAdjustBrightness(float requestBrightness) {
|
brightness.setCompoundDrawablesWithIntrinsicBounds(0,
|
R.drawable.ic_brightness, 0, 0);
|
brightness.setText("亮度:"
|
+ String.format("%1$1.0f%%", requestBrightness * 100));
|
setBrightnessVisibility(true);
|
}
|
|
@Override
|
public void showAdjustProgress(int requestProgress) {
|
if (requestProgress < 0) {
|
// progressness.setCompoundDrawablesWithIntrinsicBounds(0,
|
// R.drawable.ic_prev, 0, 0);
|
} else {
|
progressness.setCompoundDrawablesWithIntrinsicBounds(0,
|
R.drawable.ic_next, 0, 0);
|
}
|
progressness.setText(String.format("%1$+d秒", requestProgress));
|
progressness.setVisibility(View.VISIBLE);
|
}
|
|
@Override
|
public void stopAdjustProgress(int requestProgress) {
|
mTransportMediator.seekTo(mTransportMediator.getCurrentPosition()
|
+ requestProgress * 1000);
|
progressness.setVisibility(View.GONE);
|
}
|
};
|
|
private void setBrightnessVisibility(boolean visible) {
|
// If we are now visible, schedule a timer for us to go invisible.
|
if (visible) {
|
Handler h = brightness.getHandler();
|
if (h != null) {
|
h.removeCallbacks(mBrightnessHider);
|
h.postDelayed(mBrightnessHider, 1000);
|
}
|
}
|
brightness.setVisibility(visible ? View.VISIBLE : View.GONE);
|
}
|
|
private void setVolumenessVisibility(boolean visible) {
|
// If we are now visible, schedule a timer for us to go invisible.
|
if (visible) {
|
Handler h = volumeness.getHandler();
|
if (h != null) {
|
h.removeCallbacks(mVolumenessHider);
|
h.postDelayed(mVolumenessHider, 1000);
|
}
|
}
|
volumeness.setVisibility(visible ? View.VISIBLE : View.GONE);
|
}
|
|
Runnable mBrightnessHider = new Runnable() {
|
@Override
|
public void run() {
|
setBrightnessVisibility(false);
|
}
|
};
|
|
Runnable mVolumenessHider = new Runnable() {
|
@Override
|
public void run() {
|
setVolumenessVisibility(false);
|
}
|
};
|
|
private boolean isNetworkAvailable = true;
|
|
@Override
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
Bundle savedInstanceState) {
|
View view = inflater.inflate(R.layout.video_player_fragment, container,
|
false);
|
|
mChangeHelper = new ConnectivityChangeHelper(pb.getContext(),
|
new OnConnectivityChangeListener() {
|
|
@Override
|
public void onNetworkUnAvailable() {
|
isNetworkAvailable = false;
|
saveWatchHistory();
|
Toast.makeText(pb.getContext(), "网络已断开",
|
Toast.LENGTH_SHORT).show();
|
}
|
|
@Override
|
public void onWiFiAvailable() {
|
if (!isNetworkAvailable) {
|
isNetworkAvailable = true;
|
}
|
Toast.makeText(pb.getContext(), "当前为WIFI网络",
|
Toast.LENGTH_SHORT).show();
|
}
|
|
@Override
|
public void onMobileAvailable() {
|
if (!isNetworkAvailable) {
|
isNetworkAvailable = true;
|
}
|
Toast.makeText(pb.getContext(), "当前为移动网络",
|
Toast.LENGTH_SHORT).show();
|
}
|
});
|
pb = (ProgressBar) view.findViewById(R.id.probar);
|
downloadRateView = (TextView) view.findViewById(R.id.download_rate);
|
loadRateView = (TextView) view.findViewById(R.id.load_rate);
|
load_tip = (TextView) view.findViewById(R.id.load_tip);
|
brightness = (TextView) view.findViewById(R.id.brightness);
|
volumeness = (TextView) view.findViewById(R.id.volumeness);
|
audioManager = (AudioManager) pb.getContext().getSystemService(
|
Context.AUDIO_SERVICE);
|
myRegisterReceiver();
|
progressness = (TextView) view.findViewById(R.id.progressness);
|
|
// mVideoView = (BeibeiVideoView) view.findViewById(R.id.video_view);
|
// mVideoView.setErrorNoticeContent(BeibeiVideoAPI.ERROR_NOTICE);
|
// mVideoView.setBufferSize(1024*512);//512KB缓冲
|
// mVideoView.setBackgroundResource(resid);
|
|
|
// mTransportMediator = new TransportMediator(mVideoView,
|
// mTransportPerformer);
|
|
mMediaController = (BeibeiMediaController) view
|
.findViewById(R.id.media_controller);
|
|
// mMediaController.setActivity(getActivity());
|
mMediaController.setMediaPlayer(mTransportMediator);
|
mMediaController.setCallback(this);
|
mMediaController.setUseFastForward(false);
|
mediaController = mMediaController;
|
|
// mVideoView.setMediaController(mMediaController);
|
|
// mGestureMediaController = new GestureMediaController(mVideoView,
|
// mGestureTransportController);
|
// mVideoView.setGestureMediaController(mGestureMediaController);
|
|
// mVideoView.requestFocus();
|
// mVideoView.setOnInfoListener(this);
|
// mVideoView.setOnBufferingUpdateListener(this);
|
// mVideoView.setOnErrorListener(this);
|
// mVideoView.setWrapedOnCompletionListener(this);
|
// mVideoView.setWrapedOnPreparedListener(this);
|
|
onConfigurationChanged(pb.getContext().getResources().getConfiguration());
|
// showInterstitialAd();// 聚效广告
|
// initAdvertisement();// 广点通广告
|
// BtVideoPlayerFragment.initInterstitialAd(getActivity());// 百通广告
|
// if (BeibeiVideoApplication.showAd)
|
// showInterstitialAd(true);
|
return view;
|
}
|
|
MyVolumeReceiver mVolumeReceiver;
|
AudioManager audioManager;
|
|
@Override
|
public void onDestroy() {
|
super.onDestroy();
|
}
|
|
/**
|
* 注册当音量发生变化时接收的广播
|
*/
|
private void myRegisterReceiver() {
|
mVolumeReceiver = new MyVolumeReceiver();
|
IntentFilter filter = new IntentFilter();
|
filter.addAction("android.media.VOLUME_CHANGED_ACTION");
|
pb.getContext().registerReceiver(mVolumeReceiver, filter);
|
}
|
|
/**
|
* 处理音量变化时的界面显示
|
*
|
* @author long
|
*/
|
private class MyVolumeReceiver extends BroadcastReceiver {
|
@Override
|
public void onReceive(Context context, Intent intent) {
|
// 如果音量发生变化则更改seekbar的位置
|
if (intent.getAction()
|
.equals("android.media.VOLUME_CHANGED_ACTION")) {
|
int currVolume = audioManager
|
.getStreamVolume(AudioManager.STREAM_MUSIC);// 当前的媒体音量
|
if (currVolume == 0) {
|
volumeness.setCompoundDrawablesWithIntrinsicBounds(0,
|
R.drawable.ic_media_volume_mute, 0, 0);
|
BeibeiMediaController.mVolume
|
.setImageResource(R.drawable.ic_media_volume_mute);
|
} else {
|
volumeness.setCompoundDrawablesWithIntrinsicBounds(0,
|
R.drawable.ic_media_volume, 0, 0);
|
BeibeiMediaController.mVolume
|
.setImageResource(R.drawable.ic_media_volume);
|
}
|
}
|
}
|
}
|
|
public void setVideoLayout(int layout, float aspectRatio) {
|
// if (mVideoView != null) {
|
// mVideoView.setVideoLayout(layout, aspectRatio);
|
// }
|
}
|
|
// @Override
|
// public void onCompletion(MediaPlayer mp) {
|
// if (mPlayingPosition < mVideoInfo.getVideoDetailList().size() - 1) {
|
// EventBus.getDefault().post(
|
// mVideoInfo.getVideoDetailList().get(mPlayingPosition + 1));
|
// if (StringUtils.isEmpty(mVideoInfo.getVideoDetailList()
|
// .get(mPlayingPosition).getTag())) {
|
// mMediaController.setTitle(mVideoInfo.getName());
|
// } else {
|
// mMediaController.setTitle(mVideoInfo.getName()
|
// + " "
|
// + mVideoInfo.getVideoDetailList().get(mPlayingPosition)
|
// .getTag());
|
// }
|
// Toast.makeText(getActivity(), "正为你播放下一集", Toast.LENGTH_SHORT)
|
// .show();
|
// }
|
// }
|
|
int index = 0;
|
private boolean isError = false;
|
Dialog dialog;
|
|
// @Override
|
// public boolean onError(MediaPlayer mp, int what, int extra) {
|
// // try {
|
// // MobclickAgent.reportError(getActivity(), getActivity()
|
// // .getSharedPreferences("user", Context.MODE_PRIVATE)
|
// // .getString("uid", "")
|
// // + "::" + mVideoInfo.getId() + "::" + what + "::" + extra);
|
// // } catch (Exception e) {
|
// // e.printStackTrace();
|
// // }
|
// playFailedtime++;
|
// if (playFailedtime < 3) {
|
// EventBus.getDefault().post(
|
// mVideoInfo.getVideoDetailList().get(mPlayingPosition));
|
// return true;
|
// }
|
// playFailedtime = 0;
|
// BeibeiVideoAPI.setVideoError(getActivity(),
|
// getActivity()
|
// .getSharedPreferences("user", Context.MODE_PRIVATE)
|
// .getString("uid", ""), mVideoInfo.getId() + "::" + what
|
// + "::" + extra, new JsonHttpResponseHandler() {
|
// });
|
// int message = what ==
|
// MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK ?
|
// getResources()
|
// .getIdentifier(
|
// "VideoView_error_text_invalid_progressive_playback",
|
// "string", getActivity().getPackageName())
|
// : getResources().getIdentifier("VideoView_error_title",
|
// "string", getActivity().getPackageName());
|
// String da = getResources().getString(message);
|
// if (!com.lcjian.library.util.common.StringUtils
|
// .isBlank(BeibeiVideoAPI.ERROR_NOTICE)) {
|
// da = BeibeiVideoAPI.ERROR_NOTICE;
|
// }
|
//
|
// // if (index <= 1) {
|
// // dialog = new Dialog(getActivity(), R.style.ResourceDialog);
|
// // View view = LayoutInflater.from(getActivity()).inflate(
|
// // R.layout.videoplayer_dialog_item, null);
|
// // view.setOnClickListener(new OnClickListener() {
|
// //
|
// // @Override
|
// // public void onClick(View v) {
|
// // if (dialog != null && dialog.isShowing())
|
// // dialog.dismiss();
|
// // if (mVideoInfo != null) {
|
// // isError = true;
|
// // EventBus.getDefault().post(
|
// // mVideoInfo.getVideoDetailList().get(
|
// // mPlayingPosition));
|
// // }
|
// // }
|
// // });
|
// // dialog.setContentView(view);
|
// // Window dialogWindow = dialog.getWindow();
|
// // WindowManager.LayoutParams lp = dialogWindow.getAttributes();
|
// // dialogWindow.setGravity(Gravity.CENTER);
|
// // DisplayMetrics mDisplayMetrics = new DisplayMetrics();
|
// // getActivity().getWindowManager().getDefaultDisplay()
|
// // .getMetrics(mDisplayMetrics);
|
// // lp.width = mDisplayMetrics.widthPixels * 2 / 3;
|
// // lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
// // dialogWindow.setAttributes(lp);
|
// // dialog.show();
|
// // } else {
|
// // if (dialog != null && dialog.isShowing()) {
|
// // dialog.dismiss();
|
// // }
|
// new AlertDialog.Builder(getActivity())
|
// .setTitle(
|
// getResources().getIdentifier("VideoView_error_title",
|
// "string", getActivity().getPackageName()))
|
// .setMessage(da)
|
// .setPositiveButton(
|
// getResources().getIdentifier("VideoView_error_button",
|
// "string", getActivity().getPackageName()), null)
|
// .setCancelable(false).show();
|
// // }
|
// return true;
|
// }
|
|
private PlayUrl playUrl;
|
|
// @Override
|
// public void onPrepared(MediaPlayer mp) {
|
// if (mTransportMediator.getDuration() < 60 * 1000) {
|
// new GetRealPathTask2(getActivity()) {
|
// @Override
|
// protected void onPostExecute(String[] result) {
|
// if (!TextUtils.isEmpty(result[0])) {
|
// Map<String, String> map = new HashMap<String, String>();
|
// map.put("User-Agent", result[1]);
|
// mVideoView.setVideoURI(Uri.parse(result[0]), map);
|
// mVideoInfo.getVideoDetailList().get(mPlayingPosition)
|
// .getUrls().get(0).setUrl(result[0]);
|
// }
|
// };
|
// }.execute(mVideoInfo.getVideoDetailList().get(mPlayingPosition)
|
// .getId());
|
// mVideoView.setVideoPath(playUrl.getUrl());
|
// mVideoView.post(new Runnable() {
|
// @Override
|
// public void run() {
|
// mVideoView.stopPlayback();
|
// }
|
// });
|
// }
|
// XfvideoInterstitialManager.shareInstance().defaultInterstitial()
|
// .interstitialShow(true);
|
// }
|
|
// @Override
|
// public boolean onInfo(MediaPlayer mp, int what, int extra) {
|
// switch (what) {
|
// case MediaPlayer.MEDIA_INFO_BUFFERING_START:
|
// if (mVideoView.isPlaying()) {
|
// mVideoView.pause();
|
// pb.setVisibility(View.VISIBLE);
|
//
|
// downloadRateView.setText("");
|
// loadRateView.setText("");
|
// downloadRateView.setVisibility(View.VISIBLE);
|
// loadRateView.setVisibility(View.VISIBLE);
|
// }
|
// break;
|
// case MediaPlayer.MEDIA_INFO_BUFFERING_END:
|
// if (mVideoView.isValid()) {
|
// mVideoView.start();
|
// // if (isFirst) {
|
// // isFirst = false;
|
// // showInterstitialAd(true);
|
// // }
|
// }
|
// pb.setVisibility(View.GONE);
|
// downloadRateView.setVisibility(View.GONE);
|
// loadRateView.setVisibility(View.GONE);
|
// break;
|
// case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
|
// downloadRateView.setText("" + extra + "kb/s" + " ");
|
// break;
|
// }
|
// return true;
|
// }
|
|
// @Override
|
// public void onBufferingUpdate(MediaPlayer mp, int percent) {
|
// loadRateView.setText(percent + "%");
|
// if (load_tip.getVisibility() == View.VISIBLE) {
|
// load_tip.setVisibility(View.GONE);
|
// }
|
// }
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
try {
|
mMediaController.onConfigurationChanged(newConfig);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void onFullScreenClick() {
|
if (mVideoInfo != null) {
|
try {
|
if (pb.getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
|
mMediaController.isCansave(mVideoInfo.isCanSave());
|
getActivity().setRequestedOrientation(
|
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
}
|
} catch (Exception e) {
|
// mMediaController.isCansave(mVideoInfo.isCanSave());
|
// getActivity().setRequestedOrientation(
|
// ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
}
|
|
}
|
}
|
|
@Override
|
public void onChooseEpClick() {
|
// mVideoView.pause();
|
mMediaController.hide();
|
// VideoEpisodeFragment2.newInstance(mVideoInfo, mPlayingPosition).show(
|
// getActivity().getSupportFragmentManager(),
|
// "VideoEpisodeFragment2");
|
}
|
|
@Override
|
public void onShareClick() {
|
// mVideoView.clearAnimation();
|
// mVideoView.pause();
|
mMediaController.hide();
|
if (pb.getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
ShareVideoDialogFragment shareDialogFragment = new ShareVideoDialogFragment();
|
Bundle args = new Bundle();
|
args.putString("video_name", mVideoInfo.getName());
|
String path = mVideoInfo.getPicture();
|
if (!path.contains("http")) {
|
path = BeibeiConstant.HOST + "/BeiBeiVideo" + path;
|
}
|
args.putString("video_cover", path);
|
// shareDialogFragment.setArguments(args);
|
// if (shareDialogFragment != null)
|
// shareDialogFragment.show(getChildFragmentManager(),
|
// "ShareVideoDialogFragment");
|
} else {
|
ShareDialogFragment shareDialogFragment = new ShareDialogFragment();
|
Bundle args = new Bundle();
|
args.putString("video_name", mVideoInfo.getName());
|
String path = mVideoInfo.getPicture();
|
if (path != null && !path.contains("http")) {
|
path = BeibeiConstant.HOST + "/BeiBeiVideo" + path;
|
}
|
args.putString("video_cover", path);
|
// shareDialogFragment.setArguments(args);
|
// if (shareDialogFragment != null)
|
// shareDialogFragment.show(getChildFragmentManager(),
|
// "ShareDialogFragment");
|
}
|
}
|
|
@Override
|
public void onBackClick() {
|
getActivity().onBackPressed();
|
}
|
|
@Override
|
public void onFavouriteClick() {
|
SharedPreferences preferences = pb.getContext().getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
final String loginUid = preferences.getString("LoginUid", "");
|
if (StringUtils.isEmpty(loginUid)) {
|
SingleToast.showToast(brightness.getContext(), "登录后才能收藏");
|
startActivity(new Intent(brightness.getContext(), LoginActivity.class));
|
return;
|
}
|
String uid = preferences.getString("uid", "");
|
BeibeiVideoAPI.isCollect(pb.getContext(), uid, mVideoInfo.getId(),
|
mVideoInfo.getThirdType(), new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
getScoreCollect(loginUid, "0", mVideoInfo.getThirdType());
|
} else {
|
getScoreCollect(loginUid, "1", mVideoInfo.getThirdType());
|
}
|
}
|
});
|
}
|
|
@Override
|
public void onOfflineClick() {
|
VideoEpisodeFragment4.newInstance(mVideoInfo).show(
|
getChildFragmentManager(), "VideoEpisodeFragment4");
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
if (mVideoInfo != null) {
|
Cursor cursor = pb.getContext().getContentResolver().query(
|
WatchHistoryTable.CONTENT_URI,
|
null,
|
WatchHistoryTable.VIDEO_DETAIL_ID + " = ? ",
|
new String[]{mVideoInfo.getVideoDetailList()
|
.get(mPlayingPosition).getId()}, null);
|
if (cursor != null) {
|
|
// if (cursor.moveToFirst()) {
|
// mVideoView.setLastPosition(cursor.getLong(cursor
|
// .getColumnIndex(WatchHistoryTable.WATCH_TIME)));
|
// } else {
|
// mVideoView.setLastPosition(0);
|
// }
|
cursor.close();
|
}
|
}
|
EventBus.getDefault().register(this);
|
mChangeHelper.registerReceiver();
|
// mMediaController.setTitle(mVideoInfo.getName()
|
// + " "
|
// + mVideoInfo.getVideoDetailList().get(mPlayingPosition + 1)
|
// .getTag());
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
// mVideoView.pause();
|
if (isNetworkAvailable) {
|
saveWatchHistory();
|
}
|
EventBus.getDefault().unregister(this);
|
mChangeHelper.unregisterReceiver();
|
}
|
|
@Override
|
public void onDestroyView() {
|
super.onDestroyView();
|
mMediaController.clearAdThread();
|
getActivity().unregisterReceiver(mVolumeReceiver);
|
// showInterstitialAd(true);
|
// try {
|
// mVideoView.stopPlayback();
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
}
|
|
// EventBus事件响应
|
public void onEventMainThread(ContentValues values) {
|
mMediaController.setOfflined(DownloadUtils.isOffline(pb.getContext(),
|
mVideoInfo.getId(),
|
mVideoInfo.getVideoDetailList().get(mPlayingPosition).getId()));
|
}
|
|
public void onEventMainThread(ChangeVideoEvent changeVideoEvent) {
|
if ("VideoPlayerFragment".equals(changeVideoEvent.getTag())) {
|
saveWatchHistory();
|
changeVideoEvent.setTag("VideoDetailActivity");
|
EventBus.getDefault().post(changeVideoEvent);
|
}
|
}
|
|
public void onEventMainThread(VideoInfo videoInfo) {
|
if (mVideoInfo != null) {
|
isFirst = true;
|
}
|
mVideoInfo = videoInfo;
|
if (mVideoInfo.getVideoDetailList().get(mPlayingPosition).getTag() != null) {
|
mMediaController.setTitle(mVideoInfo.getName()
|
+ " "
|
+ mVideoInfo.getVideoDetailList().get(mPlayingPosition)
|
.getTag());
|
} else {
|
mMediaController.setTitle(mVideoInfo.getName());
|
}
|
if (mVideoInfo.getVideoDetailList().size() > 1) {
|
mMediaController.setPrevNextListeners(new OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
if (mPlayingPosition < mVideoInfo.getVideoDetailList()
|
.size() - 1) {
|
EventBus.getDefault().post(
|
mVideoInfo.getVideoDetailList().get(
|
mPlayingPosition + 1));
|
mMediaController.setTitle(mVideoInfo.getName()
|
+ " "
|
+ mVideoInfo.getVideoDetailList()
|
.get(mPlayingPosition).getTag());
|
} else {
|
Toast.makeText(pb.getContext(), "没有下一集了",
|
Toast.LENGTH_SHORT).show();
|
}
|
}
|
}, null);
|
}
|
isCollect();
|
}
|
|
String path1;// 播放路劲
|
String userAgent1;// 播放的userAgent
|
String path2;// 播放路劲
|
String userAgent2;// 播放的userAgent
|
String path3;// 播放路劲
|
String userAgent3;// 播放的userAgent
|
|
public void onEventMainThread(VideoDetailInfo videoDetailInfo) {
|
// 初始化
|
|
int playingPosition = 0;// 当前播放的集数
|
for (int i = 0; i < mVideoInfo.getVideoDetailList().size(); i++) {
|
if (videoDetailInfo.getId().equals(
|
mVideoInfo.getVideoDetailList().get(i).getId())) {
|
playingPosition = i;
|
break;
|
}
|
}
|
// VideoPlayUrlUtil.initVideoDetailUrl(mVideoInfo.getVideoDetailList()
|
// .get(playingPosition));
|
if (mPlayingPosition != playingPosition) {
|
index = 0;
|
}
|
if (mPlayingPosition == 0 || mPlayingPosition != playingPosition) {// 播放的第一集或者不是同一集
|
mPlayingPosition = playingPosition;
|
|
index = 2;
|
String videoId = mVideoInfo.getId();
|
String videoDetailId = mVideoInfo.getVideoDetailList()
|
.get(mPlayingPosition).getId();
|
path1 = DownloadUtils.getOfflinePath(pb.getContext(), videoId,
|
videoDetailId);
|
|
if (TextUtils.isEmpty(path1)) {
|
// path1 = VideoPlayUrlUtil.getVideoUrl(
|
// mVideoInfo.getVideoDetailList().get(mPlayingPosition))
|
// .getUrl();
|
(new GetRealPathTask(pb.getContext()) {
|
protected void onPostExecute(String[] result) {
|
userAgent1 = result[1];
|
path1 = result[0];
|
HashMap<String, String> map = new HashMap<String, String>();
|
map.put("User-Agent", result[1]);
|
// mVideoView.setVideoURI(Uri.parse(result[0]), map);
|
if (BeibeiVideoAPI.isDebug)
|
Toast.makeText(pb.getContext(), "播放路径:" + result,
|
Toast.LENGTH_LONG).show();
|
}
|
|
;
|
}).execute(path1, videoDetailId, GetRealPathTask
|
.getNeedSecondConnectServerUrl(mVideoInfo
|
.getExtraData()));
|
} else {
|
// mVideoView.setVideoPath(path1);
|
HashMap<String, String> map = new HashMap<String, String>();
|
map.put("User-Agent", userAgent1);
|
// mVideoView.setVideoURI(Uri.parse(path1), map);
|
|
if (BeibeiVideoAPI.isDebug)
|
Toast.makeText(pb.getContext(), "播放路径:" + path1,
|
Toast.LENGTH_LONG).show();
|
}
|
Cursor cursor = pb.getContext().getContentResolver().query(
|
WatchHistoryTable.CONTENT_URI, null,
|
WatchHistoryTable.VIDEO_DETAIL_ID + " = ? ",
|
new String[]{videoDetailId}, null);
|
// if (cursor.moveToFirst()) {
|
// mVideoView.setLastPosition(cursor.getLong(cursor
|
// .getColumnIndex(WatchHistoryTable.WATCH_TIME)));
|
// } else {
|
// mVideoView.setLastPosition(0);
|
// }
|
cursor.close();
|
mMediaController.setOfflined(DownloadUtils.isOffline(pb.getContext(),
|
videoId, videoDetailId));
|
|
SharedPreferences preferences = pb.getContext().getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
BeibeiVideoAPI.getScoreWatch(pb.getContext(), uid, videoDetailId,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
}
|
});
|
} else if (isError && mPlayingPosition == playingPosition) {
|
index++;
|
String videoId = mVideoInfo.getId();
|
String videoDetailId = mVideoInfo.getVideoDetailList()
|
.get(mPlayingPosition).getId();
|
path3 = DownloadUtils.getOfflinePath(pb.getContext(), videoId,
|
videoDetailId);
|
if (TextUtils.isEmpty(path3)) {
|
// path3 =
|
// VideoPlayUrlUtil.getVideoUrl(videoDetailInfo).getUrl();
|
(new GetRealPathTask(pb.getContext()) {
|
protected void onPostExecute(String[] result) {
|
// mVideoView.setVideoPath(result);
|
userAgent3 = result[1];
|
path3 = result[0];
|
HashMap<String, String> map = new HashMap<String, String>();
|
map.put("User-Agent", result[1]);
|
// mVideoView.setVideoURI(Uri.parse(result[0]), map);
|
if (BeibeiVideoAPI.isDebug)
|
Toast.makeText(pb.getContext(), "播放路径:" + result,
|
Toast.LENGTH_LONG).show();
|
}
|
|
;
|
}).execute(path3, videoDetailId);
|
} else {
|
// mVideoView.setVideoPath(path3);
|
HashMap<String, String> map = new HashMap<String, String>();
|
map.put("User-Agent", userAgent3);
|
// mVideoView.setVideoURI(Uri.parse(path3), map);
|
if (BeibeiVideoAPI.isDebug)
|
Toast.makeText(pb.getContext(), "播放路径:" + path3,
|
Toast.LENGTH_LONG).show();
|
}
|
Cursor cursor = pb.getContext().getContentResolver().query(
|
WatchHistoryTable.CONTENT_URI, null,
|
WatchHistoryTable.VIDEO_DETAIL_ID + " = ? ",
|
new String[]{videoDetailId}, null);
|
// if (cursor.moveToFirst()) {
|
// mVideoView.setLastPosition(cursor.getLong(cursor
|
// .getColumnIndex(WatchHistoryTable.WATCH_TIME)));
|
// } else {
|
// mVideoView.setLastPosition(0);
|
// }
|
cursor.close();
|
mMediaController.setOfflined(DownloadUtils.isOffline(pb.getContext(),
|
videoId, videoDetailId));
|
} else
|
// if (!VideoPlayUrlUtil.isSameVideoUrl(videoDetailInfo, mVideoInfo
|
// .getVideoDetailList().get(mPlayingPosition)))
|
{// 更换的url播放
|
saveWatchHistory();// 保存原来的播放进度
|
index = 2;
|
// 不是同一个url链接--重新播放
|
mPlayingPosition = playingPosition;
|
String videoId = mVideoInfo.getId();
|
String videoDetailId = mVideoInfo.getVideoDetailList()
|
.get(mPlayingPosition).getId();
|
|
path2 = DownloadUtils.getOfflinePath(pb.getContext(), videoId,
|
videoDetailId);
|
if (TextUtils.isEmpty(path2)) {
|
// path2 =
|
// VideoPlayUrlUtil.getVideoUrl(videoDetailInfo).getUrl();
|
(new GetRealPathTask(pb.getContext()) {
|
protected void onPostExecute(String[] result) {
|
userAgent2 = result[1];
|
path2 = result[0];
|
// mVideoView.setVideoPath(result[0]);
|
HashMap<String, String> map = new HashMap<String, String>();
|
map.put("User-Agent", result[1]);
|
// mVideoView.setVideoURI(Uri.parse(result[0]), map);
|
if (BeibeiVideoAPI.isDebug)
|
Toast.makeText(pb.getContext(), "播放路径:" + result,
|
Toast.LENGTH_LONG).show();
|
}
|
|
;
|
}).execute(path2, videoDetailId);
|
} else {
|
// mVideoView.setVideoPath(path2);
|
HashMap<String, String> map = new HashMap<String, String>();
|
map.put("User-Agent", userAgent2);
|
// mVideoView.setVideoURI(Uri.parse(path2), map);
|
if (BeibeiVideoAPI.isDebug)
|
Toast.makeText(pb.getContext(), "播放路径:" + path2,
|
Toast.LENGTH_LONG).show();
|
}
|
Cursor cursor = pb.getContext().getContentResolver().query(
|
WatchHistoryTable.CONTENT_URI, null,
|
WatchHistoryTable.VIDEO_DETAIL_ID + " = ? ",
|
new String[]{videoDetailId}, null);
|
// if (cursor.moveToFirst()) {
|
// mVideoView.setLastPosition(cursor.getLong(cursor
|
// .getColumnIndex(WatchHistoryTable.WATCH_TIME)));
|
// } else {
|
// mVideoView.setLastPosition(0);
|
// }
|
cursor.close();
|
mMediaController.setOfflined(DownloadUtils.isOffline(pb.getContext(),
|
videoId, videoDetailId));
|
}
|
if (StringUtils.isEmpty(mVideoInfo.getVideoDetailList()
|
.get(mPlayingPosition).getTag())) {
|
mMediaController.setTitle(mVideoInfo.getName());
|
} else {
|
mMediaController.setTitle(mVideoInfo.getName()
|
+ " "
|
+ mVideoInfo.getVideoDetailList().get(mPlayingPosition)
|
.getTag());
|
}
|
}
|
|
public void onEventMainThread(PlayEvent playEvent) {
|
mTransportMediator.startPlaying();
|
}
|
|
public void onEventMainThread(FavouriteEvent favouriteEvent) {
|
isCollect();
|
}
|
|
public void saveWatchHistory() {
|
if (mVideoInfo != null) {
|
String videoDetailId = mVideoInfo.getVideoDetailList()
|
.get(mPlayingPosition).getId();
|
Cursor cursor = pb.getContext().getContentResolver().query(
|
WatchHistoryTable.CONTENT_URI, null,
|
WatchHistoryTable.VIDEO_DETAIL_ID + " = ? ",
|
new String[]{videoDetailId}, null);
|
if (cursor == null) {
|
return;
|
}
|
long now = System.currentTimeMillis();
|
if (cursor.getCount() == 0) {
|
ContentValues values = new ContentValues();
|
values.put(WatchHistoryTable.VIDEO_ID, mVideoInfo.getId());
|
values.put(WatchHistoryTable.VIDEO_DETAIL_ID, videoDetailId);
|
values.put(WatchHistoryTable.VIDEO_THIRD_TYPE,
|
mVideoInfo.getThirdType());
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE).create();
|
values.put(WatchHistoryTable.VIDEO_DETAIL,
|
gson.toJson(mVideoInfo));
|
// values.put(WatchHistoryTable.WATCH_TIME,
|
// mVideoView.getCurrentPosition());
|
values.put(WatchHistoryTable.UPDATE_TIME, now);
|
values.put(WatchHistoryTable.CREATE_TIME, now);
|
pb.getContext().getContentResolver().insert(
|
WatchHistoryTable.CONTENT_URI, values);
|
} else {
|
// if (mVideoView.getCurrentPosition() != 0) {
|
// ContentValues values = new ContentValues();
|
// values.put(WatchHistoryTable.WATCH_TIME,
|
// mVideoView.getCurrentPosition());
|
// values.put(WatchHistoryTable.UPDATE_TIME, now);
|
// getActivity().getContentResolver().update(
|
// WatchHistoryTable.CONTENT_URI, values,
|
// WatchHistoryTable.VIDEO_DETAIL_ID + " = ? ",
|
// new String[] { videoDetailId });
|
// }
|
}
|
cursor.close();
|
cursor = pb.getContext().getContentResolver().query(
|
WatchHistoryTable.CONTENT_URI,
|
new String[]{WatchHistoryTable._ID}, null, null,
|
WatchHistoryTable._ID + " desc");
|
List<Long> longList = new ArrayList<Long>();
|
while (cursor.moveToNext()) {
|
longList.add(cursor.getLong(0));
|
}
|
cursor.close();
|
if (longList.size() > 30) {
|
int count = pb.getContext().getContentResolver().delete(
|
WatchHistoryTable.CONTENT_URI,
|
WatchHistoryTable._ID + "<?",
|
new String[]{longList.get(30) + ""});
|
}
|
}
|
}
|
|
private void getScoreCollect(String loginUid, final String type, String thirdType) {
|
SharedPreferences preferences = pb.getContext().getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
BeibeiVideoAPI.getScoreCollect(pb.getContext(), uid, loginUid, mVideoInfo.getId(),
|
thirdType, type, new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
if (type.equals("1")) {
|
Toast.makeText(pb.getContext(), "收藏成功",
|
Toast.LENGTH_LONG).show();
|
} else {
|
Toast.makeText(pb.getContext(), "取消收藏成功",
|
Toast.LENGTH_LONG).show();
|
}
|
EventBus.getDefault().post(new FavouriteEvent());
|
} else {
|
if (type.equals("1")) {
|
Toast.makeText(pb.getContext(), "收藏失败",
|
Toast.LENGTH_LONG).show();
|
} else {
|
Toast.makeText(pb.getContext(), "取消收藏失败",
|
Toast.LENGTH_LONG).show();
|
}
|
}
|
}
|
});
|
}
|
|
private void isCollect() {
|
SharedPreferences preferences = pb.getContext().getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
BeibeiVideoAPI.isCollect(pb.getContext(), uid, mVideoInfo.getId(),
|
mVideoInfo.getThirdType(), new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
mMediaController.setFavourited(jsonObject
|
.getBoolean("IsPost"));
|
}
|
});
|
}
|
|
/**
|
* 广点通广告
|
*/
|
private boolean isFirst = true;
|
|
private boolean isMFirst = true;
|
|
|
/**
|
* 聚效插屏广告
|
*/
|
// private IMvInterstitialAd mAdView;
|
//
|
// private void showInterstitialAd() {
|
// mAdView = Mvad.showInterstitial(getActivity(), BeibeiConstant.JXTP_ID,
|
// false);
|
// }
|
|
public static class PlayEvent {
|
|
}
|
|
public static class FavouriteEvent {
|
|
}
|
|
private ConnectivityChangeHelper mChangeHelper;
|
|
private int count;
|
|
// @Override
|
// public void onLock() {
|
// count++;
|
// if (count % 2 == 1) {// 锁屏
|
// mGestureMediaController.setEnabled(false);
|
// } else if (count % 2 == 0) {// 解锁
|
// mGestureMediaController.setEnabled(true);
|
// }
|
// }
|
}
|