阅读原文
12-传递播放列表&对象的序列化
12.1 视频列表传数据
//3.传递列表数据-对象-序列化
Intent intent = new Intent(context, SystemVideoPlayer.class);
Bundle bundle = new Bundle();
bundle.putSerializable("videolist",mediaItems);
intent.putExtras(bundle);
intent.putExtra("position",position);
context.startActivity(intent);
12.2 MediaItem
/**
* 作用:代表一个视频和音频
*/
public class MediaItem implements Serializable
{
private String name;
private long duration;
private long size;
private String data;
private String artist;
private String desc;
private String imageUrl;
public String getDesc()
{
return desc;
}
public void setDesc(String desc)
{
this.desc = desc;
}
public String getImageUrl()
{
return imageUrl;
}
public void setImageUrl(String imageUrl)
{
this.imageUrl = imageUrl;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public long getDuration()
{
return duration;
}
public void setDuration(long duration)
{
this.duration = duration;
}
public long getSize()
{
return size;
}
public void setSize(long size)
{
this.size = size;
}
public String getData()
{
return data;
}
public void setData(String data)
{
this.data = data;
}
public String getArtist()
{
return artist;
}
public void setArtist(String artist)
{
this.artist = artist;
}
@Override
public String toString()
{
return "MediaItem{" +
"name='" + name + '\'' +
", duration=" + duration +
", size=" + size +
", data='" + data + '\'' +
", artist='" + artist + '\'' +
'}';
}
}
13-监听播放完成和播放上一个和下一个视频
13.1 设置监听播放完成
//播放完成了的监听
videoview.setOnCompletionListener(new MyOnCompletionListener());
class MyOnCompletionListener implements MediaPlayer.OnCompletionListener
{
@Override
public void onCompletion(MediaPlayer mp)
{
playNextVideo();
// Toast.makeText(SystemVideoPlayer.this, "播放完成了="+uri, Toast.LENGTH_SHORT).show();
}
}
13.2 播放上一个和下一个
/**
* 设置点击事件
*/
btnVideoPre.setOnClickListener(this);
btnVideoStartPause.setOnClickListener(this);
btnVideoNext.setOnClickListener(this);
/**
* 播放下一个视频
*/
private void playNextVideo()
{
if (mediaItems != null && mediaItems.size() > 0)
{
//播放下一个
position++;
if (position < mediaItems.size())
{
ll_loading.setVisibility(View.VISIBLE);
MediaItem mediaItem = mediaItems.get(position);
tvName.setText(mediaItem.getName());
isNetUri = utils.isNetUri(mediaItem.getData());
videoview.setVideoPath(mediaItem.getData());
//设置按钮状态
setButtonState();
}
}
else if (uri != null)
{
//设置按钮状态-上一个和下一个按钮设置灰色并且不可以点击
setButtonState();
}
}
/**
* 播放上一个视频
*/
private void playPreVideo()
{
if (mediaItems != null && mediaItems.size() > 0)
{
//播放上一个视频
position--;
if (position >= 0)
{
ll_loading.setVisibility(View.VISIBLE);
MediaItem mediaItem = mediaItems.get(position);
tvName.setText(mediaItem.getName());
isNetUri = utils.isNetUri(mediaItem.getData());
videoview.setVideoPath(mediaItem.getData());
//设置按钮状态
setButtonState();
}
}
else if (uri != null)
{
//设置按钮状态-上一个和下一个按钮设置灰色并且不可以点击
setButtonState();
}
}
13.3 设置最后和第一个按钮状状态
/**
* 设置按钮状态
*/
private void setButtonState()
{
if (mediaItems != null && mediaItems.size() > 0)
{
if (mediaItems.size() == 1)
{
setEnable(false);
}
else if (mediaItems.size() == 2)
{
if (position == 0)
{
btnVideoPre.setBackgroundResource(R.drawable.btn_pre_gray);
btnVideoPre.setEnabled(false);
btnVideoNext.setBackgroundResource(R.drawable.btn_video_next_selector);
btnVideoNext.setEnabled(true);
}
else if (position == mediaItems.size() - 1)
{
btnVideoNext.setBackgroundResource(R.drawable.btn_next_gray);
btnVideoNext.setEnabled(false);
btnVideoPre.setBackgroundResource(R.drawable.btn_video_pre_selector);
btnVideoPre.setEnabled(true);
}
}
else
{
if (position == 0)
{
btnVideoPre.setBackgroundResource(R.drawable.btn_pre_gray);
btnVideoPre.setEnabled(false);
}
else if (position == mediaItems.size() - 1)
{
btnVideoNext.setBackgroundResource(R.drawable.btn_next_gray);
btnVideoNext.setEnabled(false);
}
else
{
setEnable(true);
}
}
}
else if (uri != null)
{
//两个按钮设置灰色
setEnable(false);
}
}
private void setEnable(boolean isEnable)
{
if (isEnable)
{
btnVideoPre.setBackgroundResource(R.drawable.btn_video_pre_selector);
btnVideoPre.setEnabled(true);
btnVideoNext.setBackgroundResource(R.drawable.btn_video_next_selector);
btnVideoNext.setEnabled(true);
}
else
{
//两个按钮设置灰色
btnVideoPre.setBackgroundResource(R.drawable.btn_pre_gray);
btnVideoPre.setEnabled(false);
btnVideoNext.setBackgroundResource(R.drawable.btn_next_gray);
btnVideoNext.setEnabled(false);
}
}
14-播放器控制面板控制面
14.1 定义手势识别器
//1.定义一个手势识别器
private GestureDetector detector;
14.2 实例化手势识别器
//2.实例化手势识别器,并且重写双击,点击,长按
detector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener()
{
@Override
public void onLongPress(MotionEvent e)
{
super.onLongPress(e);
// Toast.makeText(SystemVideoPlayer.this, "我被长按了", Toast.LENGTH_SHORT).show();
startAndPause();
}
@Override
public boolean onDoubleTap(MotionEvent e)
{
// Toast.makeText(SystemVideoPlayer.this, "我被双击了", Toast.LENGTH_SHORT).show();
setFullScreenAndDefault();
return super.onDoubleTap(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
// Toast.makeText(SystemVideoPlayer.this, "我被单击了", Toast.LENGTH_SHORT).show();
if (isshowMediaController)
{
//隐藏
hideMediaController();
//把隐藏消息移除
handler.removeMessages(HIDE_MEDIACONTROLLER);
}
else
{
//显示
showMediaController();
//发消息隐藏
handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROLLER, 4000);
}
return super.onSingleTapConfirmed(e);
}
});
14.3 把点击事件传入手势识别器中
@Override
public boolean onTouchEvent(MotionEvent event)
{
//3.把事件传递给手势识别器
detector.onTouchEvent(event);
14.4 隐藏和显示控制面板方法
/**
* 显示控制面板
*/
private void showMediaController()
{
media_controller.setVisibility(View.VISIBLE);
isshowMediaController = true;
}
/**
* 隐藏控制面板
*/
private void hideMediaController()
{
media_controller.setVisibility(View.GONE);
isshowMediaController = false;
}
14.5 过段时间自动隐藏方法和移除消息
/**
* 发一个延迟600才隐藏控制面板的消息
*/
private void sendDelayedHideControlPlayerMessage()
{
handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROLLER, 4000);
}
/**
* 在消息队列中移除隐藏控制面板的消息
*/
private void removeDelayedHideControlPlayerMessage()
{
handler.removeMessages(DELAYED_HIDECONTROL_MESSAGE);
}
14.6 拖动视频的时候也要隐藏
//设置SeeKbar状态变化的监听
seekbarVideo.setOnSeekBarChangeListener(new VideoOnSeekBarChangeListener());
class VoiceOnSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener
{
//手指滑动状态发送变化的时候回调
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if (fromUser)
{
if (progress > 0)
{
isMute = false;
}
else
{
isMute = true;
}
updataVoice(progress, isMute);
}
}
//手指刚开始滑动的时候回调
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
handler.removeMessages(HIDE_MEDIACONTROLLER);
}
//手指停止滑动的时候回调
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROLLER, 4000);
}
}
15-自定义VideoView
15.1 自定义VideoView-增加设置视频大小方法
/**
* 作用:自定义VideoView,用于切换视频大小
*/
public class VideoView extends android.widget.VideoView
{
/**
* 在代码中创建的时候一般用这个方法
*
* @param context
*/
public VideoView(Context context)
{
this(context, null);
}
/**
* 当这个类在布局文件的时候,系统通过该构造方法实例化该类
*
* @param context
* @param attrs
*/
public VideoView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
/**
* 当需要设置样式的时候调用该方法
*
* @param context
* @param attrs
* @param defStyleAttr
*/
public VideoView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
/**
* 设置视频的宽和高 \n
* by Hashub
*
* @param videoWidth 指定视频的宽
* @param videoHeight 指定视频的高
*/
public void setVideoSize(int videoWidth, int videoHeight)
{
ViewGroup.LayoutParams params = getLayoutParams();
params.width = videoWidth;
params.height = videoHeight;
setLayoutParams(params);
}
}
15.2 在SystemVideoPlayer中得到屏幕高和宽方法
//得到屏幕的宽和高
//过时的方式
//screenWidth = getWindowManager().getDefaultDisplay().getWidth();
//screenHeight = getWindowManager().getDefaultDisplay().getHeight();
//得到屏幕的宽和高最新方式
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels;
3 15.3 视频默认和全屏
/**设置屏幕全屏或默认大小
* true全屏
* flase默认
*/
/**
private void setFullScreenAndDefault()
{
if (isFullScreen)
{
//默认
setVideoType(DEFAULT_SCREEN);
}
else
{
//全屏
setVideoType(FULL_SCREEN);
}
}
/**
* 设置视频播放类型
*
* @param defaultScreen
*/
private void setVideoType(int defaultScreen)
{
switch (defaultScreen)
{
case FULL_SCREEN://全屏
//1.设置视频画面的大小-屏幕有多大就是多大
videoview.setVideoSize(screenWidth, screenHeight);
//2.设置按钮的状态-默认
btnVideoSiwchScreen.setBackgroundResource(R.drawable.btn_video_siwch_screen_default_selector);
isFullScreen = true;
break;
case DEFAULT_SCREEN://默认
//1.设置视频画面的大小
//视频真实的宽和高
int mVideoWidth = videoWidth;
int mVideoHeight = videoHeight;
//屏幕的宽和高
int width = screenWidth;
int height = screenHeight;
// for compatibility, we adjust size based on aspect ratio
if (mVideoWidth * height < width * mVideoHeight)
{
//Log.i("@@@", "image too wide, correcting");
width = height * mVideoWidth / mVideoHeight;
}
else if (mVideoWidth * height > width * mVideoHeight)
{
//Log.i("@@@", "image too tall, correcting");
height = width * mVideoHeight / mVideoWidth;
}
videoview.setVideoSize(width, height);
//2.设置按钮的状态--全屏
btnVideoSiwchScreen.setBackgroundResource(R.drawable.btn_video_siwch_screen_full_selector);
isFullScreen = false;
break;
}
}
15.4-屏幕保持不锁屏
//设置屏幕不锁屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
15.5 点击按钮的时候实现切换播放模式
else if (v == btnVideoSiwchScreen)
{
setFullScreenAndDefault();
}
16-用AudioManager调声音-15
16.1 得到当前音量和最大音量
/**
* 调用声音
*/
private AudioManager am;
/**
* 当前的音量
*/
private int currentVoice;
/**
* 0~15
* 最大音量
*/
private int maxVoice;
/**
* 是否是静音
*/
private boolean isMute = false;
//得到音量
am = (AudioManager) getSystemService(AUDIO_SERVICE);
currentVoice = am.getStreamVolume(AudioManager.STREAM_MUSIC);
maxVoice = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
16.2 拖动的时候改变音量
seekbarVoice.setOnSeekBarChangeListener(new VoiceOnSeekBarChangeListener());
class VoiceOnSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener
{
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if (fromUser)
{
if (progress > 0)
{
isMute = false;
}
else
{
isMute = true;
}
updataVoice(progress, isMute);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
handler.removeMessages(HIDE_MEDIACONTROLLER);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROLLER, 4000);
}
}
/**
* 设置音量的大小
*
* @param progress
*/
private void updataVoice(int progress, boolean isMute)
{
if (isMute)
{
am.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
seekbarVoice.setProgress(0);
}
else
{
am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
seekbarVoice.setProgress(progress);
currentVoice = progress;
}
}
16.3 静音和非静音
if (v == btnVoice)
{
isMute = !isMute;
updataVoice(currentVoice, isMute);
}
17-手指在屏幕上下滑动实现声音大小变化
private float startY;
/**
* 滑动的区域
*/
private float touchRang;
/**
* 当前的音量
*/
private int mVol;
//接收事件,传递给手识别器
@Override
public boolean onTouchEvent(MotionEvent event)
{
//3.把事件传递给手势识别器
detector.onTouchEvent(event);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN://手指按下
//1.按下记录值
startY = event.getY();
mVol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
touchRang = Math.min(screenHeight, screenWidth);//screenHeight
handler.removeMessages(HIDE_MEDIACONTROLLER);
break;
case MotionEvent.ACTION_MOVE://手指移动
//2.移动的记录相关值
float endY = event.getY();
float distanceY = startY - endY;
//改变声音 = (滑动屏幕的距离: 总距离)*音量最大值
float delta = (distanceY / touchRang) * maxVoice;
//最终声音 = 原来的 + 改变声音;
int voice = (int) Math.min(Math.max(mVol + delta, 0), maxVoice);
if (delta != 0)
{
isMute = false;
updataVoice(voice, isMute);
}
// startY = event.getY();//不要加
break;
case MotionEvent.ACTION_UP://手指离开
handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROLLER, 4000);
break;
}
return super.onTouchEvent(event);
}
18-监听按手机声音健改变声音
/**
* 监听物理健,实现声音的调节大小
*
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
currentVoice--;
updataVoice(currentVoice, false);
handler.removeMessages(HIDE_MEDIACONTROLLER);
handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROLLER, 4000);
return true;
}
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
currentVoice++;
updataVoice(currentVoice, false);
handler.removeMessages(HIDE_MEDIACONTROLLER);
handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROLLER, 4000);
return true;
}
return super.onKeyDown(keyCode, event);
}
网友评论