| | |
| | | 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 MediaPlayer mediaPlayer; |
| | | private IVideoPlayerListener videoPlayerListener; |
| | | private List<VideoEpisode> episodeList; |
| | | private String videoName; |
| | | private String videoUrl; |
| | | private boolean collected;//是否收藏 |
| | | |
| | | private Timer timer; |
| | | |
| | | |
| | | //容器 |
| | |
| | | |
| | | 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 TextView tv_progress;//现在的播放进度 |
| | | private TextView tv_duration;//视频总时长 |
| | | private TextView tv_definition;//清晰度 |
| | | private ImageView iv_episode;//选集 |
| | | private ImageView iv_full_screen; |
| | | private TextView tv_resource; |
| | | |
| | | public VideoPlayerController setMediaPlayer(MediaPlayer mediaPlayer) { |
| | | this.mediaPlayer = mediaPlayer; |
| | | return this; |
| | | } |
| | | private boolean needNotifyMobileData = true; |
| | | |
| | | public VideoPlayerController setVideoPlayerListener(IVideoPlayerListener videoPlayerListener) { |
| | | this.videoPlayerListener = videoPlayerListener; |
| | | return this; |
| | | } |
| | | //视频清晰度 |
| | | private List<VideoDefinition> videoDefinitionList; |
| | | |
| | | public VideoPlayerController setContentView(View contentView) { |
| | | this.contentView = contentView; |
| | | return this; |
| | | } |
| | | //当前清晰度 |
| | | private VideoDefinition currentDefinition; |
| | | |
| | | private ImageView iv_collection, iv_download, iv_share, iv_episode;//收藏,下载,分享,选集 |
| | | |
| | | //是否可以隐藏 |
| | | private boolean canHiddenController = true; |
| | | |
| | | //正在播放的位置 |
| | | private int mPlayingPosition; |
| | | |
| | | 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); |
| | |
| | | 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) { |
| | | mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
| | | 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 videoUrl |
| | | * @param playingPosition |
| | | * @return |
| | | */ |
| | | public VideoPlayerController setVideoInfo(List<VideoEpisode> episodeList, String videoName, String videoUrl, int playingPosition) { |
| | | this.videoName = videoName; |
| | | this.videoUrl = videoUrl; |
| | | this.episodeList = episodeList; |
| | | this.mPlayingPosition = playingPosition; |
| | | 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; |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | |
| | | } |
| | | }).build(mActivity); |
| | | if (!networkNotiftDialog.isShowing()) |
| | | networkNotiftDialog.show(); |
| | | } |
| | | 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(); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 设置电量变化 |
| | | */ |
| | | 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); |
| | | } |
| | | iv_lock.setVisibility(View.VISIBLE); |
| | | delayHiddenController(); |
| | | } |
| | | |
| | | private void hiddenController() { |
| | | fl_bottom.setVisibility(View.GONE); |
| | | ll_top.setVisibility(View.GONE); |
| | | iv_lock.setVisibility(View.GONE); |
| | | 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 positionLong = mediaPlayer.getDuration() * seekBar.getProgress() / 1000; |
| | | 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(Activity mActivity) { |
| | | public void build(final Activity mActivity, Fragment fragment) { |
| | | this.mActivity = mActivity; |
| | | this.fragment = fragment; |
| | | setLayout(); |
| | | tv_title.setText(videoName); |
| | | tv_link.setText(videoUrl); |
| | | 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) { |
| | |
| | | return true; |
| | | } |
| | | }); |
| | | seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
| | | seekBar.setOnSeekBarChangeListener(seekBarChangeListener); |
| | | aq.id(R.id.seekBar_portrait).getSeekBar().setOnSeekBarChangeListener(seekBarChangeListener); |
| | | |
| | | boolean isTouch = false; |
| | | |
| | | iv_collection.setOnClickListener(new OnClickListener() { |
| | | @Override |
| | | public void onProgressChanged(SeekBar seekBar, int precent, boolean b) { |
| | | if (isTouch) { |
| | | //计算 拖拉后的 时间长度: |
| | | int positionLong = mediaPlayer.getDuration() * seekBar.getProgress() / 100; |
| | | mediaPlayer.seekTo(positionLong); |
| | | public void onClick(View v) { |
| | | if (videoPlayerListener != null) {//收藏 |
| | | videoPlayerListener.collect(!collected); |
| | | } |
| | | } |
| | | |
| | | }); |
| | | iv_download.setOnClickListener(new OnClickListener() { |
| | | @Override |
| | | public void onStartTrackingTouch(SeekBar seekBar) { |
| | | // mHandler.removeMessages(0); |
| | | // isTouch = true; |
| | | // mediaPlayer.pause(); |
| | | // stop$play.setImageResource(R.mipmap.play_small); |
| | | } |
| | | |
| | | @Override |
| | | public void onStopTrackingTouch(SeekBar seekBar) { |
| | | // if (controllerView.getVisibility() == View.VISIBLE) { |
| | | // if (mHandler != null) { |
| | | // mHandler.sendEmptyMessageDelayed(0, 4000); |
| | | // } |
| | | // } |
| | | isTouch = false; |
| | | mediaPlayer.start(); |
| | | // stop$play.setImageResource(R.mipmap.stop_small); |
| | | 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, 1).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(); |
| | | VideoResourceUtil.setPlayImage(iv_play, true, isLand); |
| | | } else { |
| | | mediaPlayer.start(); |
| | | VideoResourceUtil.setPlayImage(iv_play, false, isLand); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | |
| | | 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(); |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | public void changeOrien(Activity activity) { |
| | | // boolean tag = requestOrien.getTag() == null ? false : (boolean) requestOrien.getTag(); |
| | | // activity.setRequestedOrientation(!tag ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
| | | // requestOrien.setTag(!tag); |
| | | 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 void lock() { |
| | | hiddenController(); |
| | | private boolean isLand = false; |
| | | |
| | | /** |
| | | * 屏幕方向改变 |
| | | * |
| | | * @param land |
| | | */ |
| | | public void changeOrien(boolean land) { |
| | | if (isLand != land) { |
| | | //更改屏幕方向 |
| | | isLand = land; |
| | | setLayout(); |
| | | } |
| | | } |
| | | |
| | | //解锁屏 |
| | | private void unLock() { |
| | | if (isLock()) |
| | | showController(); |
| | | private void 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 / mediaPlayer.getDuration())); |
| | | setPlayerState(); |
| | | } |
| | | 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) { |
| | | if (mediaPlayer.isPlaying()) { |
| | | VideoResourceUtil.setPlayImage(iv_play, false, isLand); |
| | | aq.id(R.id.iv_play_center).visibility(View.GONE); |
| | | } else { |
| | | VideoResourceUtil.setPlayImage(iv_play, true, isLand); |
| | | if (!isLand) { |
| | | aq.id(R.id.iv_play_center).visibility(View.VISIBLE); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //暂停播放 |
| | | private synchronized void pausePlay() { |
| | | if (mediaPlayer != null && mediaPlayer.isPlaying()) |
| | | mediaPlayer.pause(); |
| | | setPlayerState(); |
| | | |
| | | } |
| | | |
| | | private synchronized void continuePlay() { |
| | | if (mediaPlayer != null && !mediaPlayer.isPlaying()) |
| | | mediaPlayer.start(); |
| | | setPlayerState(); |
| | | } |
| | | |
| | | |
| | | interface IVideoPlayerListener { |
| | | //点击了暂停按钮 |
| | | public void onPause(); |
| | | 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 onDestory() { |
| | | if (timer != null) |
| | | timer.cancel(); |
| | | } |
| | | |
| | | |
| | | public interface IVideoPlayerListener { |
| | | //暂停 |
| | | public void onVideoPause(); |
| | | |
| | | //播放 |
| | | public void onVideoPlay(); |
| | | |
| | | //下一集 |
| | | public void onNext(); |
| | | |
| | | //返回 |
| | | public void onBack(); |
| | | } |
| | | |
| | | private boolean isLock() { |
| | | return false; |
| | | } |
| | | //选集 |
| | | public void selectEpisode(int p, VideoEpisode episode); |
| | | |
| | | private boolean isControllerShow() { |
| | | return ll_top.getVisibility() == View.VISIBLE; |
| | | //搜藏 |
| | | public void collect(boolean collect); |
| | | |
| | | //切换清晰度 |
| | | public void changeDefinition(int p, VideoDefinition definition); |
| | | } |
| | | |
| | | //手势处理 |
| | |
| | | } |
| | | |
| | | @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 (distanceX != 0) { |
| | | |
| | | 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); |
| | | } |
| | | |
| | | } |
| | | |