使用Vitamio打造自己的Android万能播放器(1)——准备
一、实现目标
1.1亮度控制
模仿VPlayer界面:
1.2声音控制
模仿VPlayer界面:
1.3画面缩放
根据下面API提供画面的拉伸、剪切、100%、全屏
二、Vitamio API 介绍
VideoView
2.1public void start()
开始播放
2.2public void pause()
暂停播放
2.3public long getDuration()
获取视频的时长
2.4public long getCurrentPosition()
获取已经播放的时长
2.5public void seekTo(long msec)
设置播放器从指定的位置开始播放
2.6public boolean isPlaying()
是否正在播放
2.7public int getVideoWidth()
获取视频宽
2.8public int getVideoHeight()
获取视频高
2.9public void setBufferSize(int bufSize)
设置缓存大小,默认1024KB
2.10public void setVideoQuality(int quality)
设置视频质量,低、中、高(MediaPlayer.VIDEOQUALITY_LOW、MediaPlayer.VIDEOQUALITY_MEDIUM 、MediaPlayer.VIDEOQUALITY_HIGH ),
默认低(最流畅)。
2.11public void setSubShown(boolean shown)
设置是否显示字幕
2.12public void setAudioTrack(int audioIndex)
设置音轨,必须是getAudioTrackMap(String) 的返回值。
2.13public void setVolume(float leftVolume, float rightVolume)
设置立体音左右音量。
2.14public void setSubPath(String subPath)
设置外挂字幕路径
2.15public int getBufferPercentage()
获取缓冲百分比
2.16public void stopPlayback()
停止播放
2.17public void setVideoPath(String path)
设置视频播放路径
2.18public void setVideoURI(Uri uri)
设置视频播放路径
2.19public void setVideoLayout(int layout, float aspectRatio)
设置视频缩放(拉伸、剪切、100%、全屏)
三、 实现代码
3.1xml
<?
xml version="1.0" encoding="utf-8"
?>
<
RelativeLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
>
<
io.vov.vitamio.widget.VideoView
android:id
="@+id/surface_view"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:layout_centerHorizontal
="true"
android:layout_centerVertical
="true"
/>
<
FrameLayout
android:id
="@+id/operation_volume_brightness"
android:visibility
="invisible"
android:layout_centerInParent
="true"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:background
="#00000000"
android:orientation
="horizontal"
android:padding
="0dip"
>
<
ImageView
android:id
="@+id/operation_bg"
android:layout_gravity
="center"
android:src
="@drawable/video_volumn_bg"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
/>
<
FrameLayout
android:layout_gravity
="bottom|center_horizontal"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:paddingBottom
="25dip"
>
<
ImageView
android:id
="@+id/operation_full"
android:layout_gravity
="left"
android:src
="@drawable/video_num_bg"
android:layout_width
="94dip"
android:layout_height
="wrap_content"
/>
<
ImageView
android:id
="@+id/operation_percent"
android:layout_gravity
="left"
android:src
="@drawable/video_num_front"
android:layout_width
="0dip"
android:layout_height
="wrap_content"
android:scaleType
="matrix"
/>
</
FrameLayout
>
</
FrameLayout
>
</RelativeLayout>
3.2Activity
/**
*
* Android万能播放器
*
*
@author
农民伯伯
*
@version
2012-5-22
*
*/
public
class VideoViewDemo
extends Activity {
private String path = Environment.getExternalStorageDirectory()
+ "/Moon.mp4";
private VideoView mVideoView;
private View mVolumeBrightnessLayout;
private ImageView mOperationBg;
private ImageView mOperationPercent;
private AudioManager mAudioManager;
/**
最大声音
*/
private
int mMaxVolume;
/**
当前声音
*/
private
int mVolume = -1;
/**
当前亮度
*/
private
float mBrightness = -1f;
/**
当前缩放模式
*/
private
int mLayout = VideoView.VIDEO_LAYOUT_ZOOM;
private GestureDetector mGestureDetector;
private MediaController mMediaController;
@Override
public
void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVolumeBrightnessLayout = findViewById(R.id.operation_volume_brightness);
mOperationBg = (ImageView) findViewById(R.id.operation_bg);
mOperationPercent = (ImageView) findViewById(R.id.operation_percent);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mMaxVolume = mAudioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mVideoView.setVideoPath(path);
mMediaController =
new MediaController(
this);
mVideoView.setMediaController(mMediaController);
mVideoView.requestFocus();
mGestureDetector =
new GestureDetector(
this,
new MyGestureListener());
}
@Override
public
boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event))
return
true;
//
处理手势结束
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
endGesture();
break;
}
return
super.onTouchEvent(event);
}
/**
手势结束
*/
private
void endGesture() {
mVolume = -1;
mBrightness = -1f;
//
隐藏
mDismissHandler.removeMessages(0);
mDismissHandler.sendEmptyMessageDelayed(0, 500);
}
private
class MyGestureListener
extends SimpleOnGestureListener {
/**
双击
*/
@Override
public
boolean onDoubleTap(MotionEvent e) {
if (mLayout == VideoView.VIDEO_LAYOUT_ZOOM)
mLayout = VideoView.VIDEO_LAYOUT_ORIGIN;
else
mLayout++;
if (mVideoView !=
null)
mVideoView.setVideoLayout(mLayout, 0);
return
true;
}
/**
滑动
*/
@Override
public
boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX,
float distanceY) {
float mOldX = e1.getX(), mOldY = e1.getY();
int y = (
int) e2.getRawY();
Display disp = getWindowManager().getDefaultDisplay();
int windowWidth = disp.getWidth();
int windowHeight = disp.getHeight();
if (mOldX > windowWidth * 4.0 / 5)
//
右边滑动
onVolumeSlide((mOldY - y) / windowHeight);
else
if (mOldX < windowWidth / 5.0)
//
左边滑动
onBrightnessSlide((mOldY - y) / windowHeight);
return
super.onScroll(e1, e2, distanceX, distanceY);
}
}
/**
定时隐藏
*/
private Handler mDismissHandler =
new Handler() {
@Override
public
void handleMessage(Message msg) {
mVolumeBrightnessLayout.setVisibility(View.GONE);
}
};
/**
* 滑动改变声音大小
*
*
@param
percent
*/
private
void onVolumeSlide(
float percent) {
if (mVolume == -1) {
mVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
if (mVolume < 0)
mVolume = 0;
//
显示
mOperationBg.setImageResource(R.drawable.video_volumn_bg);
mVolumeBrightnessLayout.setVisibility(View.VISIBLE);
}
int index = (
int) (percent * mMaxVolume) + mVolume;
if (index > mMaxVolume)
index = mMaxVolume;
else
if (index < 0)
index = 0;
//
变更声音
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);
//
变更进度条
ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();
lp.width = findViewById(R.id.operation_full).getLayoutParams().width
* index / mMaxVolume;
mOperationPercent.setLayoutParams(lp);
}
/**
* 滑动改变亮度
*
*
@param
percent
*/
private
void onBrightnessSlide(
float percent) {
if (mBrightness < 0) {
mBrightness = getWindow().getAttributes().screenBrightness;
if (mBrightness <= 0.00f)
mBrightness = 0.50f;
if (mBrightness < 0.01f)
mBrightness = 0.01f;
//
显示
mOperationBg.setImageResource(R.drawable.video_brightness_bg);
mVolumeBrightnessLayout.setVisibility(View.VISIBLE);
}
WindowManager.LayoutParams lpa = getWindow().getAttributes();
lpa.screenBrightness = mBrightness + percent;
if (lpa.screenBrightness > 1.0f)
lpa.screenBrightness = 1.0f;
else
if (lpa.screenBrightness < 0.01f)
lpa.screenBrightness = 0.01f;
getWindow().setAttributes(lpa);
ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();
lp.width = (
int) (findViewById(R.id.operation_full).getLayoutParams().width * lpa.screenBrightness);
mOperationPercent.setLayoutParams(lp);
}
@Override
public
void onConfigurationChanged(Configuration newConfig) {
if (mVideoView !=
null)
mVideoView.setVideoLayout(mLayout, 0);
super.onConfigurationChanged(newConfig);
}
}
3.3代码说明
3.3.1 缩放功能
该功能SDK已经提供好了接口,直接使用即可。
3.3.2 音量和亮度控制实现
根据layout可以看得出,利用FrameLayout的特点(后面视图会覆盖前面视图),通过控制后一个视图的宽度来达到进度条的效果。
3.3.3 自动隐藏
可用Handle来实现自定延时隐藏的功能,比较实用。
3.3.4 手势
手势方面大家可用多查查GestureDetector方面的资料,双击、缩放手势都可以实现。
四、代码下载
请移步#Taocode(SVN):(没有账户的请注册一个账户即可。)
项目地址:http://code.taobao.org/p/oplayer
五、Vitamio相关信息
5.1近期将发布新的SDK版本
5.1.1 将直接内置各平台解码器,无需外下载!
5.1.2 将支持自定义进度控制条等。
六、相关文章
6.1Android 播放电影时滑动屏幕调整屏幕亮度
6.2android MediaPlayer API