在上一个笔记里已经把IjkVideoView改造成简版的轮子了,现在来写符合自己需求的控制器。
我本次项目中主要写了以下功能:开始、暂停、调整播放进度、全屏、退出全屏等。
首先得创建自己的控制器布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/btn_play"
android:layout_width="@dimen/dp48"
android:layout_height="@dimen/dp48"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@color/transparent"
android:scaleType="centerInside"
android:src="@mipmap/pause" />
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/black_dark"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="@dimen/dp4"
android:paddingLeft="@dimen/dp10"
android:paddingRight="@dimen/dp10"
android:paddingTop="@dimen/dp4">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tv_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textColor="@color/white"
android:textSize="@dimen/sp12" />
<android.support.v7.widget.AppCompatSeekBar
android:id="@+id/seek_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/dp10"
android:paddingRight="@dimen/dp10" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tv_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textColor="@color/white"
android:textSize="@dimen/sp12" />
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/btn_full"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp10"
android:background="@drawable/full_screen"
android:button="@null"
android:checked="false" />
</android.support.v7.widget.LinearLayoutCompat>
</RelativeLayout>
然后定义自己的控制器。控制器需要实现 IMediaController接口。循规蹈矩的完成IMediaController各个方法的功能后,就可以添加自己的方法了。不啰嗦,直接贴代码:
package com.wym.mario.widget.media;
import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.AppCompatSeekBar;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.MediaController;
import android.widget.SeekBar;
import com.wym.mario.R;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import java.util.Locale;
/**
* Everyday is another day, keep going.
* Created by Ramo
* email: 327300401@qq.com
* date: 2017/10/17 09:28
* desc:
*/
public class MyMediaController implements IMediaController, View.OnClickListener, AppCompatCheckBox.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {
private View controllerRoot;
private AppCompatTextView tvStart;
private AppCompatTextView tvEnd;
private AppCompatSeekBar seekBar;
private AppCompatCheckBox btnFull;
private AppCompatImageButton btnPlay;
private boolean isShowing;
private MediaController.MediaPlayerControl player;
private long duration;
private boolean isDragging;
private AudioManager audioManager;
public MyMediaController(Context context) {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
controllerRoot = LayoutInflater.from(context).inflate(R.layout.video_controller, null);
tvStart = controllerRoot.findViewById(R.id.tv_start);
tvEnd = controllerRoot.findViewById(R.id.tv_end);
seekBar = controllerRoot.findViewById(R.id.seek_bar);
btnFull = controllerRoot.findViewById(R.id.btn_full);
btnPlay = controllerRoot.findViewById(R.id.btn_play);
btnFull.setOnCheckedChangeListener(this);
btnPlay.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(this);
}
public void attach(IVideoView videoView) {
videoView.addView(controllerRoot);
videoView.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer iMediaPlayer) {
isDragging = false;
isShowing = true;
handler.sendEmptyMessage(MESSAGE_SHOW_PROGRESS);
handler.sendEmptyMessageDelayed(SET_VIEW_HIDE, TIME_OUT);
}
});
videoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(IMediaPlayer iMediaPlayer) {
btnPlay.setImageResource(R.mipmap.play);
}
});
}
public void disAttach(IVideoView videoView) {
videoView.removeView(controllerRoot);
handler.removeMessages(SET_VIEW_HIDE);
handler.removeMessages(SET_VIEW_SHOW);
handler.removeMessages(MESSAGE_SHOW_PROGRESS);
handler.removeMessages(PAUSE_IMAGE_HIDE);
handler.removeMessages(MESSAGE_SEEK_NEW_POSITION);
handler.removeMessages(MESSAGE_HIDE_CONTROL);
handler = null;
}
@Override
public void hide() {
handler.removeMessages(SET_VIEW_HIDE);
handler.removeMessages(SET_VIEW_SHOW);
handler.sendEmptyMessage(SET_VIEW_HIDE);
}
@Override
public boolean isShowing() {
return isShowing;
}
@Override
public void setAnchorView(View view) {
}
@Override
public void setEnabled(boolean enabled) {
}
@Override
public void setMediaPlayer(MediaController.MediaPlayerControl player) {
this.player = player;
}
@Override
public void show(int timeout) {
handler.removeMessages(SET_VIEW_HIDE);
handler.removeMessages(SET_VIEW_SHOW);
handler.sendEmptyMessage(SET_VIEW_SHOW);
handler.sendEmptyMessageDelayed(SET_VIEW_HIDE, timeout);
}
@Override
public void show() {
handler.removeMessages(SET_VIEW_HIDE);
handler.removeMessages(SET_VIEW_SHOW);
handler.sendEmptyMessage(SET_VIEW_SHOW);
handler.sendEmptyMessageDelayed(SET_VIEW_HIDE, TIME_OUT);
}
@Override
public void showOnce(View view) {
}
@Override
public void onClick(View v) {
int id = v.getId();
//切换播放状态
if (id == R.id.btn_play) {
if (player.isPlaying()) {
pause();
} else {
reStart();
}
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mListener != null) {
mListener.onFullScreen(isChecked);
}
}
//全屏与非全屏转换
public void switchFull(boolean full) {
btnFull.setChecked(full);
}
//是否全屏
public boolean isFull() {
return btnFull.isChecked();
}
public void pause() {
btnPlay.setImageResource(R.mipmap.play);
player.pause();
if (mListener != null) {
mListener.onPause(true);
}
}
public void reStart() {
btnPlay.setImageResource(R.mipmap.pause);
player.start();
if (mListener != null) {
mListener.onPause(false);
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String string = generateTime(progress);
tvStart.setText(string);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
setProgress();
isDragging = true;
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
handler.removeMessages(MESSAGE_SHOW_PROGRESS);
show();
handler.removeMessages(SET_VIEW_HIDE);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isDragging = false;
player.seekTo(seekBar.getProgress());
handler.removeMessages(MESSAGE_SHOW_PROGRESS);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
isDragging = false;
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_PROGRESS, 1000);
show();
}
private String generateTime(long time) {
int totalSeconds = (int) (time / 1000);
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
return hours > 0 ? String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds)
: String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
}
private void setProgress() {
if (isDragging) {
return;
}
long position = player.getCurrentPosition();
long duration = player.getDuration();
this.duration = duration;
if (!generateTime(duration).equals(tvEnd.getText().toString()))
tvEnd.setText(generateTime(duration));
if (seekBar != null) {
if (duration > 0) {
int pos = (int) position;
seekBar.setMax((int) duration);
seekBar.setProgress(pos);
}
int percent = player.getBufferPercentage();
seekBar.setSecondaryProgress(percent);
}
String string = generateTime(position);
tvStart.setText(string);
}
private static final int SET_VIEW_HIDE = 1;
private static final int SET_VIEW_SHOW = 6;
private static final int TIME_OUT = 3000;
private static final int MESSAGE_SHOW_PROGRESS = 2;
private static final int PAUSE_IMAGE_HIDE = 3;
private static final int MESSAGE_SEEK_NEW_POSITION = 4;
private static final int MESSAGE_HIDE_CONTROL = 5;
private long newPosition = 1;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SET_VIEW_HIDE:
controllerRoot.setVisibility(View.GONE);
isShowing = false;
break;
case SET_VIEW_SHOW:
controllerRoot.setVisibility(View.VISIBLE);
isShowing = true;
break;
case MESSAGE_SHOW_PROGRESS:
setProgress();
if (!isDragging) {
msg = obtainMessage(MESSAGE_SHOW_PROGRESS);
sendMessageDelayed(msg, 1000);
}
break;
case PAUSE_IMAGE_HIDE:
break;
case MESSAGE_SEEK_NEW_POSITION:
if (newPosition >= 0) {
player.seekTo((int) newPosition);
newPosition = -1;
}
break;
case MESSAGE_HIDE_CONTROL:
// seekTxt.setVisibility(View.GONE);
break;
}
}
};
//自定义的接口,可以根据自己的需求修改
private OnVideoListener mListener;
public void setListener(OnVideoListener listener) {
mListener = listener;
}
public interface OnVideoListener {
void onPause(boolean pause);
void onFullScreen(boolean full);
}
}
最后就是如何在项目中使用了。比较建议使用代码创建IVideoView,附上我自己的初始化代码:
video = new IVideoView(this);
video.setVideoUrl("视频地址");
controller = new MyMediaController(this);
controller.setListener(new MyMediaController.OnVideoListener() {
@Override
public void onPause(boolean pause) {
}
@Override
public void onFullScreen(boolean full) {
LinearLayoutCompat parent = (LinearLayoutCompat) video.getParent();
if (full) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//做自己想做的操作
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//做自己想做的操作
}
}
});
controller.attach(video);
video.setMediaController(controller);
video.setBackgroundColor(getResources().getColor(R.color.black));
video.setLoop(false); // 设置不循环播放
video.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
llParent.addView(video);
video.start();
另外需要处理一下进入后台,重新回到前台和界面销毁的一些操作:
@Override
protected void onResume() {
super.onResume();
if (video != null) {
video.seekTo(lastPosition);
video.resume();
}
}
@Override
protected void onPause() {
super.onPause();
if (video != null && video.isPlaying()) {
lastPosition = video.getCurrentPosition();
video.pause();
}
}
@Override
public void onBackPressed() {
if (controller.isFull()) {
controller.switchFull(false);
} else {
super.onBackPressed();
}
}
@Override
protected void onDestroy() {
if (video != null && video.isDrawingCacheEnabled()) {
video.destroyDrawingCache();
}
if (controller != null) {
controller.disAttach(video);
}
IjkMediaPlayer.native_profileEnd();
super.onDestroy();
}
到此结束。