package com.lcjian.library.video;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.pm.ActivityInfo;
|
import android.graphics.Color;
|
import android.media.AudioManager;
|
import android.media.MediaPlayer;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
import android.support.annotation.NonNull;
|
import android.support.annotation.Nullable;
|
import android.support.v4.app.Fragment;
|
import android.util.AttributeSet;
|
import android.util.Log;
|
import android.view.GestureDetector;
|
import android.view.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.MotionEvent;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.view.WindowManager;
|
import android.view.animation.Animation;
|
import android.view.animation.AnimationUtils;
|
import android.view.animation.LinearInterpolator;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.PopupWindow;
|
import android.widget.SeekBar;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.androidquery.AQuery;
|
import com.lcjian.lcjianlibrary.R;
|
import com.lcjian.library.entity.eventbus.PlayerControllerShow;
|
import com.lcjian.library.entity.eventbus.PlayerLock;
|
import com.lcjian.library.util.common.DimenUtils;
|
import com.lcjian.library.util.common.TimeUtil;
|
|
import java.util.List;
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
import de.greenrobot.event.EventBus;
|
|
import static android.content.Context.AUDIO_SERVICE;
|
|
public class VideoPlayerController extends FrameLayout {
|
|
private Activity mActivity;
|
|
private final static String TAG = "VideoPlayerController";
|
|
private MediaPlayer mediaPlayer;
|
private IVideoPlayerListener videoPlayerListener;
|
private List<VideoEpisode> episodeList;
|
private String videoName;
|
private String videoUrl;
|
private boolean collected;//是否收藏
|
private String mResourceName;
|
|
private Timer timer;
|
|
|
//容器
|
private FrameLayout fl_bottom;
|
private LinearLayout ll_top;
|
private View contentView;
|
|
|
private ImageView iv_back;//返回
|
private TextView tv_title;//标题
|
private TextView tv_link;//链接
|
private ImageView iv_electric;//电量
|
private TextView tv_time;//当前时间
|
private ImageView iv_lock;//锁屏
|
private ImageView iv_play;//播放
|
private SeekBar seekBar;//进度
|
private ImageView iv_next;//下一级
|
private TextView tv_progress;//现在的播放进度
|
private TextView tv_duration;//视频总时长
|
private TextView tv_definition;//清晰度
|
private ImageView iv_full_screen;
|
private TextView tv_resource;
|
|
private boolean needNotifyMobileData = true;
|
|
//视频清晰度
|
private List<VideoDefinition> videoDefinitionList;
|
|
//当前清晰度
|
private VideoDefinition currentDefinition;
|
|
private ImageView iv_collection, iv_download, iv_share, iv_episode;//收藏,下载,分享,选集
|
|
//是否可以隐藏
|
private boolean canHiddenController = true;
|
|
//正在播放的位置
|
private int mPlayingPosition;
|
|
private int mShowType;//1-长条 2-小方格
|
|
private AQuery aq;
|
|
public VideoPlayerController(@NonNull Context context) {
|
super(context);
|
timer = new Timer();
|
}
|
|
public VideoPlayerController(@NonNull Context context, @Nullable AttributeSet attrs) {
|
super(context, attrs);
|
timer = new Timer();
|
LayoutInflater.from(context).inflate(R.layout.video_player_controller, this);
|
aq = new AQuery(this);
|
|
|
fl_bottom = findViewById(R.id.fl_bottom);
|
ll_top = findViewById(R.id.ll_top);
|
|
iv_back = findViewById(R.id.iv_back);
|
tv_title = findViewById(R.id.tv_title);
|
tv_link = findViewById(R.id.tv_link);
|
iv_electric = findViewById(R.id.iv_electric);
|
tv_time = findViewById(R.id.tv_time);
|
iv_lock = findViewById(R.id.iv_lock);
|
iv_play = findViewById(R.id.iv_play);
|
seekBar = findViewById(R.id.seekBar);
|
iv_next = findViewById(R.id.iv_next);
|
tv_progress = findViewById(R.id.tv_progress);
|
tv_duration = findViewById(R.id.tv_duration);
|
tv_definition = findViewById(R.id.tv_definition);
|
iv_episode = findViewById(R.id.iv_episode);
|
iv_full_screen = findViewById(R.id.iv_full_screen);
|
iv_collection = findViewById(R.id.iv_collection);
|
iv_download = findViewById(R.id.iv_download);
|
iv_share = findViewById(R.id.iv_share);
|
iv_episode = findViewById(R.id.iv_episode);
|
|
tv_resource = findViewById(R.id.tv_resource);
|
|
iv_full_screen.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (mActivity != null)
|
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
}
|
});
|
//定时器
|
timer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
if (tv_time != null) {
|
tv_time.post(new Runnable() {
|
@Override
|
public void run() {
|
if (tv_time != null)
|
tv_time.setText(TimeUtil.getGeneralTime(System.currentTimeMillis(), "HH:mm"));
|
}
|
});
|
|
}
|
}
|
}, 0, 1000);
|
|
initLoading();
|
|
showController();
|
}
|
|
//加载框初始化
|
private void initLoading() {
|
ImageView imageView = findViewById(R.id.iv_loading);
|
Animation operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_dialog_player_loading);
|
LinearInterpolator lin = new LinearInterpolator();
|
operatingAnim.setInterpolator(lin);
|
imageView.setAnimation(operatingAnim);
|
operatingAnim.start();
|
}
|
|
|
public VideoPlayerController setVideoPlayerListener(IVideoPlayerListener videoPlayerListener) {
|
this.videoPlayerListener = videoPlayerListener;
|
return this;
|
}
|
|
/**
|
* 设置视频信息
|
*
|
* @param episodeList
|
* @param videoName
|
* @param playingPosition
|
* @return
|
*/
|
public VideoPlayerController setVideoInfo(List<VideoEpisode> episodeList, int playingPosition, int showType, String resourceName) {
|
VideoEpisode currentVideoEpisode = episodeList.get(playingPosition);
|
this.videoName = currentVideoEpisode.getName();
|
this.videoUrl = currentVideoEpisode.getVideoUrl();
|
this.episodeList = episodeList;
|
this.mPlayingPosition = playingPosition;
|
this.mShowType = showType;
|
this.mResourceName = resourceName;
|
return this;
|
}
|
|
public VideoPlayerController setContentView(View contentView) {
|
this.contentView = contentView;
|
return this;
|
}
|
|
|
/**
|
* 是否收藏
|
*
|
* @param select
|
*/
|
public void setCollect(boolean select) {
|
if (select)
|
iv_collection.setImageResource(R.drawable.ic_player_collected);
|
else
|
iv_collection.setImageResource(R.drawable.ic_player_collecte);
|
}
|
|
/**
|
* 设置清晰度
|
*
|
* @param definitionList
|
* @param currentDefinition
|
*/
|
public void setDefinition(List<VideoDefinition> definitionList, VideoDefinition currentDefinition) {
|
this.videoDefinitionList = definitionList;
|
this.currentDefinition = currentDefinition;
|
if (currentDefinition != null) {
|
tv_definition.setText("清晰度:" + currentDefinition.getName());
|
if (isLand) {
|
tv_definition.setVisibility(View.VISIBLE);
|
}
|
}
|
}
|
|
//网络提示弹框
|
private VideoDataNotifyDialog networkNotiftDialog;
|
|
/**
|
* TODO
|
* 设置网络状态
|
*
|
* @param networkInfo
|
*/
|
//设置当前网络状态
|
public synchronized void setNetworkInfo(NetworkInfo networkInfo, boolean firstSet) {
|
//说明当前有网络
|
if (networkInfo != null && networkInfo.isAvailable()) {
|
if (mActivity == null)
|
return;
|
int type = networkInfo.getType();
|
switch (type) {
|
case ConnectivityManager.TYPE_MOBILE:
|
//提示是否继续播放
|
if (needNotifyMobileData) {
|
pausePlay();
|
if (networkNotiftDialog == null)
|
networkNotiftDialog = new VideoDataNotifyDialog.Builder().setDialogClick(new VideoDataNotifyDialog.IDialogClick() {
|
@Override
|
public void continuePlay() {
|
VideoPlayerController.this.continuePlay();
|
//不需要提醒
|
needNotifyMobileData = false;
|
setPlayImage();
|
}
|
|
@Override
|
public void close() {
|
|
}
|
}).build(mActivity);
|
if (!networkNotiftDialog.isShowing())
|
networkNotiftDialog.show();
|
} else {
|
continuePlay();
|
}
|
break;
|
case ConnectivityManager.TYPE_WIFI:
|
if (networkNotiftDialog != null && networkNotiftDialog.isShowing())
|
networkNotiftDialog.dismiss();
|
if (firstSet)
|
VideoPlayerController.this.continuePlay();
|
break;
|
case ConnectivityManager.TYPE_ETHERNET:
|
break;
|
}
|
} else {
|
pausePlay();
|
}
|
|
//设置播放按钮的状态按钮的
|
setPlayImage();
|
}
|
|
private void setPlayImage() {
|
if (iv_play != null)
|
iv_play.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
if (mediaPlayer == null) {
|
return;
|
}
|
VideoResourceUtil.setPlayImage(iv_play, mediaPlayer.isPlaying(), isLand);
|
if (mediaPlayer.isPlaying()) {
|
aq.id(R.id.iv_play_center).visibility(View.GONE);
|
} else {
|
if (!isLand) {
|
aq.id(R.id.iv_play_center).visibility(View.VISIBLE);
|
}
|
}
|
}
|
}, 100);
|
}
|
|
|
/**
|
* 设置电量变化
|
*/
|
public void setBattery(int percent) {
|
int resourceId;
|
if (percent < 10)
|
resourceId = R.drawable.ic_battery_1;
|
else if (percent < 20)
|
resourceId = R.drawable.ic_battery_2;
|
else if (percent < 30)
|
resourceId = R.drawable.ic_battery_3;
|
else if (percent < 40)
|
resourceId = R.drawable.ic_battery_4;
|
else if (percent < 50)
|
resourceId = R.drawable.ic_battery_5;
|
else if (percent < 60)
|
resourceId = R.drawable.ic_battery_6;
|
else if (percent < 70)
|
resourceId = R.drawable.ic_battery_7;
|
else if (percent < 80)
|
resourceId = R.drawable.ic_battery_8;
|
else
|
resourceId = R.drawable.ic_battery_10;
|
iv_electric.setImageResource(resourceId);
|
}
|
|
|
private void showController() {
|
if (!isLock()) {
|
fl_bottom.setVisibility(View.VISIBLE);
|
ll_top.setVisibility(View.VISIBLE);
|
iv_lock.setVisibility(View.VISIBLE);
|
EventBus.getDefault().post(new PlayerControllerShow(true));
|
} else {
|
fl_bottom.setVisibility(View.GONE);
|
ll_top.setVisibility(View.GONE);
|
iv_lock.setVisibility(View.VISIBLE);
|
}
|
delayHiddenController();
|
}
|
|
private void hiddenController() {
|
if (canHiddenController) {
|
if (definitionPopupWindow == null || !definitionPopupWindow.isShowing()) {
|
fl_bottom.setVisibility(View.GONE);
|
ll_top.setVisibility(View.GONE);
|
iv_lock.setVisibility(View.GONE);
|
EventBus.getDefault().post(new PlayerControllerShow(false));
|
}
|
}
|
}
|
|
private void delayHiddenController() {
|
fl_bottom.removeCallbacks(controllerHiddenRunnable);
|
fl_bottom.postDelayed(controllerHiddenRunnable, 4000);
|
}
|
|
private GestureDetector mGesde = new GestureDetector(new MSimpleGestureDectListener());
|
|
|
private VideoEpisodeDialog episodeDialog;
|
|
private PopupWindow definitionPopupWindow;
|
|
private Fragment fragment;
|
|
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
|
|
boolean isTouch = false;
|
|
@Override
|
public void onProgressChanged(SeekBar seekBar, int precent, boolean b) {
|
if (isTouch) {
|
//计算 拖拉后的 时间长度:
|
int process = mediaPlayer.getDuration();
|
float f = seekBar.getProgress() / 1000.0f;
|
int positionLong = (int) (process * f);
|
Log.i(TAG, "position:" + positionLong);
|
mediaPlayer.seekTo(positionLong);
|
}
|
|
}
|
|
@Override
|
public void onStartTrackingTouch(SeekBar seekBar) {
|
isTouch = true;
|
mediaPlayer.pause();
|
canHiddenController = false;
|
}
|
|
@Override
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
isTouch = false;
|
mediaPlayer.start();
|
int currentPosition = getSeconds(mediaPlayer.getCurrentPosition());
|
tv_progress.setText(TimeUtil.getTimeDesc(currentPosition));
|
canHiddenController = true;
|
delayHiddenController();
|
}
|
};
|
|
/**
|
* 初始化
|
*/
|
public void build(final Activity mActivity, Fragment fragment) {
|
this.mActivity = mActivity;
|
this.fragment = fragment;
|
setLayout();
|
tv_title.setText(videoName);
|
tv_link.setText(videoUrl);
|
tv_resource.setText("来源:" + mResourceName);
|
setCollect(this.collected);
|
|
showLoading();
|
|
iv_back.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (videoPlayerListener != null)
|
videoPlayerListener.onBack();
|
}
|
});
|
|
|
contentView.setOnTouchListener(new OnTouchListener() {
|
@Override
|
public boolean onTouch(View v, MotionEvent event) {
|
mGesde.onTouchEvent(event);
|
return true;
|
}
|
});
|
seekBar.setOnSeekBarChangeListener(seekBarChangeListener);
|
aq.id(R.id.seekBar_portrait).getSeekBar().setOnSeekBarChangeListener(seekBarChangeListener);
|
|
iv_collection.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (videoPlayerListener != null) {//收藏
|
videoPlayerListener.collect(!collected);
|
}
|
}
|
});
|
iv_download.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
Toast.makeText(getContext(), "暂不支持下载", Toast.LENGTH_SHORT).show();
|
}
|
});
|
|
iv_episode.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
//当前集
|
episodeDialog = new VideoEpisodeDialog.Builder().setEpisodes(episodeList, mPlayingPosition, mShowType).setDialogClick(new VideoEpisodeDialog.IDialogClick() {
|
@Override
|
public void selectEpisode(int p, VideoEpisode ve) {
|
if (videoPlayerListener != null)
|
videoPlayerListener.selectEpisode(p, ve);
|
if (episodeDialog.isShowing())
|
episodeDialog.dismiss();
|
}
|
|
@Override
|
public void close() {
|
if (episodeDialog.isShowing())
|
episodeDialog.dismiss();
|
}
|
}).build(mActivity);
|
episodeDialog.show();
|
hiddenController();
|
}
|
});
|
|
|
iv_play.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (mediaPlayer != null) {
|
if (mediaPlayer.isPlaying()) {
|
mediaPlayer.pause();
|
} else {
|
mediaPlayer.start();
|
}
|
setPlayerState();
|
}
|
}
|
});
|
|
definitionPopupWindow = null;
|
//清晰度选择
|
tv_definition.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (definitionPopupWindow == null) {
|
definitionPopupWindow = new PopupWindow(getContext());
|
}
|
if (definitionPopupWindow.isShowing())
|
definitionPopupWindow.dismiss();
|
else {
|
final View view = LayoutInflater.from(getContext()).inflate(R.layout.item_player_definition, null);
|
LinearLayout ll_container = view.findViewById(R.id.ll_container);
|
fillDefinition(ll_container);
|
|
definitionPopupWindow.setContentView(view);
|
definitionPopupWindow.setOutsideTouchable(true);
|
definitionPopupWindow.setBackgroundDrawable(null);
|
definitionPopupWindow.setFocusable(true);
|
//防止再次弹出
|
definitionPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
|
@Override
|
public boolean onTouch(View v, MotionEvent event) {
|
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
|
definitionPopupWindow.dismiss();
|
return true;
|
}
|
return false;
|
}
|
});
|
//清晰度选择消失后需要隐藏控制层
|
definitionPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
|
@Override
|
public void onDismiss() {
|
delayHiddenController();
|
}
|
});
|
|
tv_definition.post(new Runnable() {
|
@Override
|
public void run() {
|
Log.i(TAG, String.format("width:%s height:%s", view.getWidth(), view.getHeight()));
|
//保存anchor在屏幕中的位置
|
int[] location = new int[2];
|
//保存anchor上部中点
|
int[] anchorCenter = new int[2];
|
//读取位置anchor座标
|
tv_definition.getLocationOnScreen(location);
|
//计算anchor中点
|
anchorCenter[0] = location[0] + tv_definition.getWidth() / 2 - DimenUtils.dip2px(getContext(), 60);
|
anchorCenter[1] = location[1] - videoDefinitionList.size() * DimenUtils.dip2px(getContext(), 28);
|
int xoff = 0;
|
int yoff = 0;
|
|
definitionPopupWindow.showAtLocation(tv_definition, Gravity.TOP | Gravity.LEFT, anchorCenter[0] + xoff, anchorCenter[1] - (yoff));
|
}
|
});
|
|
}
|
}
|
});
|
|
iv_lock.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (iv_lock.getTag() == null) {
|
iv_lock.setImageResource(R.drawable.ic_player_lock);
|
iv_lock.setTag("lock");
|
showController();
|
EventBus.getDefault().post(new PlayerLock(true));
|
} else {
|
iv_lock.setTag(null);
|
iv_lock.setImageResource(R.drawable.ic_player_unlock);
|
showController();
|
EventBus.getDefault().post(new PlayerLock(false));
|
}
|
}
|
});
|
|
iv_next.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (videoPlayerListener != null)
|
videoPlayerListener.onNext();
|
}
|
});
|
|
//继续播放
|
aq.id(R.id.iv_play_center).clicked(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
continuePlay();
|
}
|
});
|
|
}
|
|
private void fillDefinition(final LinearLayout ll_container) {
|
ll_container.removeAllViews();
|
for (int i = 0; i < videoDefinitionList.size(); i++) {
|
final int p = i;
|
VideoDefinition vd = videoDefinitionList.get(i);
|
final VideoDefinition fvd = vd;
|
TextView tv = new TextView(getContext());
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(DimenUtils.dip2px(getContext(), 60), DimenUtils.dip2px(getContext(), 28));
|
tv.setLayoutParams(params);
|
tv.setGravity(Gravity.CENTER);
|
tv.setTextSize(12);
|
tv.setTextColor(Color.WHITE);
|
tv.setText(vd.getName());
|
tv.setBackgroundColor(Color.parseColor("#80000000"));
|
if (currentDefinition != null && currentDefinition.getName().equalsIgnoreCase(vd.getName())) {
|
tv.setTextColor(Color.parseColor("#FF078D"));
|
tv.setBackgroundColor(Color.parseColor("#800052F6"));
|
}
|
tv.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
currentDefinition = fvd;
|
tv_definition.setText("清晰度:" + currentDefinition.getName());
|
fillDefinition(ll_container);
|
videoPlayerListener.changeDefinition(p, currentDefinition);
|
if (definitionPopupWindow != null && definitionPopupWindow.isShowing())
|
definitionPopupWindow.dismiss();
|
}
|
});
|
|
ll_container.addView(tv);
|
}
|
|
}
|
|
private boolean isLand = false;
|
|
/**
|
* 屏幕方向改变
|
*
|
* @param land
|
*/
|
public void changeOrien(boolean land) {
|
if (isLand != land) {
|
//更改屏幕方向
|
isLand = land;
|
setLayout();
|
}
|
}
|
|
private void setLayout() {
|
Log.i(TAG, "setLayout");
|
if (isLand) {
|
findViewById(R.id.fl_state).setVisibility(View.VISIBLE);
|
// if (iv_collection.getVisibility() == View.GONE)
|
// iv_collection.setVisibility(View.VISIBLE);
|
if (iv_download.getVisibility() == View.GONE)
|
iv_download.setVisibility(View.VISIBLE);
|
// if (iv_share.getVisibility() == View.GONE)
|
// iv_share.setVisibility(View.VISIBLE);
|
if (iv_episode.getVisibility() == View.GONE)
|
iv_episode.setVisibility(View.VISIBLE);
|
iv_full_screen.setVisibility(View.GONE);
|
if (this.currentDefinition != null)
|
tv_definition.setVisibility(View.VISIBLE);
|
tv_resource.setVisibility(View.VISIBLE);
|
iv_next.setVisibility(View.VISIBLE);
|
|
aq.id(R.id.seekBar_portrait).visibility(View.GONE);
|
aq.id(R.id.seekBar).visibility(View.VISIBLE);
|
aq.id(R.id.view_bottom_fill).visibility(View.VISIBLE);
|
aq.id(R.id.tv_time_cutline).visibility(View.VISIBLE);
|
|
seekBar.setProgress(aq.id(R.id.seekBar).getSeekBar().getProgress());
|
} else {
|
findViewById(R.id.fl_state).setVisibility(View.GONE);
|
if (iv_collection.getVisibility() == View.VISIBLE)
|
iv_collection.setVisibility(View.GONE);
|
|
if (iv_download.getVisibility() == View.VISIBLE)
|
iv_download.setVisibility(View.GONE);
|
|
if (iv_share.getVisibility() == View.VISIBLE)
|
iv_share.setVisibility(View.GONE);
|
|
if (iv_episode.getVisibility() == View.VISIBLE)
|
iv_episode.setVisibility(View.GONE);
|
|
iv_full_screen.setVisibility(View.VISIBLE);
|
|
tv_definition.setVisibility(View.GONE);
|
tv_resource.setVisibility(View.GONE);
|
iv_next.setVisibility(View.GONE);
|
|
aq.id(R.id.seekBar_portrait).visibility(View.VISIBLE);
|
aq.id(R.id.seekBar).visibility(View.GONE);
|
aq.id(R.id.view_bottom_fill).visibility(View.GONE);
|
aq.id(R.id.tv_time_cutline).visibility(View.GONE);
|
|
aq.id(R.id.seekBar).getSeekBar().setProgress(seekBar.getProgress());
|
}
|
VideoResourceUtil.setPlayImage(iv_play, mediaPlayer != null ? mediaPlayer.isPlaying() : false, isLand);
|
}
|
|
private int getSeconds(int ms) {
|
return Math.round(ms / 1000.0f);
|
}
|
|
private Runnable playerProcessRunnable = new Runnable() {
|
@Override
|
public void run() {
|
//设置时间
|
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
|
int currentPosition = getSeconds(mediaPlayer.getCurrentPosition());
|
tv_progress.setText(TimeUtil.getTimeDesc(currentPosition));
|
seekBar.setProgress(Math.round(mediaPlayer.getCurrentPosition() * 1000.0f / mediaPlayer.getDuration()));
|
Log.i(TAG, Math.abs(mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition()) + "");
|
if (Math.abs(mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition()) < 1000) {//播放完成
|
tv_progress.removeCallbacks(playerProcessRunnable);
|
iv_next.performClick();
|
return;
|
}
|
}
|
tv_progress.postDelayed(playerProcessRunnable, 1000);
|
}
|
};
|
|
private Runnable controllerHiddenRunnable = new Runnable() {
|
@Override
|
public void run() {
|
hiddenController();
|
}
|
};
|
|
/**
|
* 视频准备好的回调
|
*
|
* @param mp
|
*/
|
public void onPrepared(MediaPlayer mp) {
|
closeLoading();
|
this.mediaPlayer = mp;
|
final int duration = getSeconds(mp.getDuration());
|
Log.i(TAG, "视频总时长:" + duration);
|
if (mp != null) {
|
tv_duration.post(new Runnable() {
|
@Override
|
public void run() {
|
tv_duration.setText(TimeUtil.getTimeDesc(duration));
|
}
|
});
|
tv_progress.removeCallbacks(playerProcessRunnable);
|
tv_progress.post(playerProcessRunnable);
|
ConnectivityManager connectivityManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
|
setNetworkInfo(networkInfo, true);
|
}
|
}
|
|
/**
|
* 设置播放状态
|
*/
|
|
private void setPlayerState() {
|
if (mediaPlayer != null) {
|
setPlayImage();
|
}
|
}
|
|
//暂停播放
|
private synchronized void pausePlay() {
|
if (mediaPlayer != null && mediaPlayer.isPlaying())
|
mediaPlayer.pause();
|
setPlayerState();
|
|
}
|
|
private synchronized void continuePlay() {
|
if (mediaPlayer != null && !mediaPlayer.isPlaying())
|
mediaPlayer.start();
|
setPlayerState();
|
}
|
|
|
private boolean isLock() {
|
Object lock = iv_lock.getTag();
|
if (lock != null) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
private boolean isControllerShow() {
|
return iv_lock.getVisibility() == View.VISIBLE;
|
}
|
|
final float FLIP_DISTANCE = 20;
|
|
|
/**
|
* 亮度调节
|
*
|
* @param brightness
|
*/
|
public void setBrightness(float brightness) {
|
if (mActivity == null)
|
return;
|
WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
|
lp.screenBrightness = lp.screenBrightness + brightness / 255.0f;
|
if (lp.screenBrightness > 1) {
|
lp.screenBrightness = 1;
|
} else if (lp.screenBrightness < 0.05) {
|
lp.screenBrightness = (float) 0.05;
|
}
|
mActivity.getWindow().setAttributes(lp);
|
}
|
|
/**
|
* 音量调节
|
*
|
* @param volume
|
*/
|
public void setAudio(int volume) {
|
AudioManager audioManager = (AudioManager) getContext().getSystemService(AUDIO_SERVICE);
|
//当前音量
|
int k = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
|
//最大音量
|
int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
|
Log.d("==d==", "" + max);
|
Log.d("==d==", "" + k);
|
k = k + volume;
|
if (k >= 0 && k <= max) {
|
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, k, AudioManager.FLAG_PLAY_SOUND);
|
}
|
}
|
|
public synchronized void showLoading() {
|
aq.id(R.id.fl_loading).visibility(View.VISIBLE);
|
}
|
|
public synchronized void closeLoading() {
|
aq.id(R.id.fl_loading).visibility(View.GONE);
|
}
|
|
public void onResume() {
|
setLayout();
|
}
|
|
public void onPause() {
|
setLayout();
|
}
|
|
/**
|
* 释放资源
|
*/
|
public void onDestory() {
|
if (timer != null)
|
timer.cancel();
|
}
|
|
|
public interface IVideoPlayerListener {
|
//暂停
|
public void onVideoPause();
|
|
//播放
|
public void onVideoPlay();
|
|
//下一集
|
public void onNext();
|
|
//返回
|
public void onBack();
|
|
//选集
|
public void selectEpisode(int p, VideoEpisode episode);
|
|
//搜藏
|
public void collect(boolean collect);
|
|
//切换清晰度
|
public void changeDefinition(int p, VideoDefinition definition);
|
}
|
|
//手势处理
|
private class MSimpleGestureDectListener extends GestureDetector.SimpleOnGestureListener {
|
|
@Override
|
public boolean onSingleTapConfirmed(MotionEvent e) {
|
if (isControllerShow()) {
|
hiddenController();
|
} else {
|
showController();
|
}
|
|
return true;
|
}
|
|
@Override
|
public boolean onDoubleTapEvent(MotionEvent e) {
|
|
return true;
|
}
|
|
@Override
|
public boolean onDoubleTap(MotionEvent e) {
|
if (mediaPlayer != null) {
|
if (mediaPlayer.isPlaying()) {
|
pausePlay();
|
} else {
|
continuePlay();
|
}
|
}
|
return true;
|
}
|
|
@Override
|
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
|
Log.i(TAG, String.format("onScroll:起始点坐标:%s,%s", e1.getX(), e1.getY()));
|
Log.i(TAG, String.format("onScroll:distanceX=%s distanceY=%s", distanceX, distanceY));
|
|
if (Math.abs(distanceX) > FLIP_DISTANCE) {//横向滑动
|
if (distanceX < 0)//右滑
|
{
|
Log.i(TAG, "右滑");
|
} else {//左滑
|
Log.i(TAG, "左滑");
|
}
|
} else if (Math.abs(distanceY) > FLIP_DISTANCE) {//竖向滑动
|
boolean left;
|
if (mActivity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {//横屏
|
if (e1.getX() < DimenUtils.getScreenHeight(mActivity) / 2) {
|
left = true;
|
} else {
|
left = false;
|
}
|
} else {//竖屏
|
if (e1.getX() < DimenUtils.getScreenWidth(mActivity) / 2) {
|
left = true;
|
} else {
|
left = false;
|
}
|
}
|
|
if (left) {
|
Log.i(TAG, "左边");
|
if (distanceY < 0)//下滑
|
{
|
Log.i(TAG, "下滑");
|
} else {//上滑
|
Log.i(TAG, "上滑");
|
}
|
//调节亮度
|
setBrightness(distanceY);
|
} else {
|
Log.i(TAG, "右边");
|
if (distanceY < 0)//下滑
|
{
|
Log.i(TAG, "下滑");
|
} else {//上滑
|
Log.i(TAG, "上滑");
|
}
|
|
//调节音量
|
setAudio((int) distanceY / 50);
|
}
|
|
}
|
|
return true;
|
}
|
}
|
|
|
}
|