Vitamio库使用起来非常简单,其api的使用和android自身的VideoView的api是一样的,Vitamio 能够流畅播放720P甚至1080P高清MKV,FLV,MP4,MOV,TS,RMVB等常见格式的视频,还可以在 Android 与 iOS 上跨平台支持 MMS, RTSP, RTMP, HLS(m3u8) 等常见的多种视频流媒体协议,包括点播与直播。

在使用vitamio库之前,先介绍下android自身的videoView的使用,因为其api的用法和vitamio的是一样的,当android自身的videoView整体功能实现完毕后,只需要切换下相关api的包名为vitamio库的即可.

1.布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><!--自定义的VideoView--><mchenys.net.csdn.blog.myplayer.common.widget.VideoViewandroid:id="@id/videoView"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerInParent="true"/><!--顶部控制面板--><LinearLayoutandroid:id="@id/ll_top_ctrl"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:orientation="vertical"><!--状态栏--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/bg_player_status"android:gravity="center_vertical"android:orientation="horizontal"android:paddingLeft="@dimen/padding5"android:paddingRight="@dimen/padding5"><!--视频名称--><TextViewandroid:id="@id/tv_title"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:textColor="@color/white"android:textSize="13sp"tools:text="video01"/><!--系统电量--><ImageViewandroid:id="@id/iv_battery"android:layout_width="wrap_content"android:layout_height="match_parent"android:src="@drawable/ic_battery_20"/><!--系统时间--><TextViewandroid:id="@id/tv_system_time"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginLeft="@dimen/margin5"android:textColor="@color/white"android:textSize="13sp"tools:text="00:00:00"/></LinearLayout><!--音量控制面板--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_player_top_control"android:gravity="center_vertical"android:orientation="horizontal"><!--静音控制按钮--><ImageButtonandroid:id="@id/ib_voice"android:layout_width="wrap_content"android:layout_height="match_parent"android:background="@null"android:src="@drawable/selector_ib_voice"/><!--音量进度--><SeekBarandroid:id="@id/sb_voice"style="@android:style/Widget.SeekBar"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginRight="6dp"android:maxHeight="3dp"android:minHeight="3dp"android:progressDrawable="@drawable/progress_horizontal"android:thumb="@drawable/seekbar_thumb"/></LinearLayout></LinearLayout><!--底部控制面板--><LinearLayoutandroid:id="@id/ll_bottom_ctrl"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:orientation="vertical"><!--播放进度--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/bg_player_bottom_seekbar"android:gravity="center_vertical"android:orientation="horizontal"android:paddingLeft="@dimen/padding5"android:paddingRight="@dimen/padding5"><!--当前播放的位置--><TextViewandroid:id="@id/tv_current_position"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/white"android:textSize="13sp"tools:text="00:00"/><!--当前播放的进度--><SeekBarandroid:id="@id/sb_video"style="@android:style/Widget.SeekBar"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginRight="6dp"android:layout_weight="1"android:maxHeight="3dp"android:minHeight="3dp"android:progressDrawable="@drawable/progress_horizontal"android:thumb="@drawable/seekbar_thumb"/><!--视频的总时长--><TextViewandroid:id="@id/tv_duration"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/white"android:textSize="13sp"tools:text="00:00"/></LinearLayout><!--播放控制--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/bg_player_bottom_control"android:gravity="center_vertical"android:orientation="horizontal"><!--返回键--><ImageButtonandroid:id="@id/ib_back"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@null"android:src="@drawable/selector_ib_back"/><!--上一个--><ImageButtonandroid:id="@id/ib_pre"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@null"android:src="@drawable/selector_ib_pre"/><!--播放/暂停--><ImageButtonandroid:id="@id/ib_play"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@null"android:src="@drawable/selector_ib_play"/><!--下一个--><ImageButtonandroid:id="@id/ib_next"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@null"android:src="@drawable/selector_ib_next"/><!--全屏/按比例--><ImageButtonandroid:id="@id/ib_fullScreen"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@null"android:src="@drawable/selector_ib_full_screen"/></LinearLayout></LinearLayout><!--通过透明度改变屏幕亮度的view--><Viewandroid:background="@color/black"android:id="@id/view_brightness"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="gone"/>
</RelativeLayout>

效果图如下:

2.关于SeekBar的进度的背景图片和滑动按钮的图片都是可以自定制的,通过progressDrawable属性可以设置进度的背景图片,这个属性在RatingBar和ProgressBar上都有,效果都是一样的.通过thumb属性可以修改SeekBar进度按钮的背景图片,这里顺便贴出res/drawable/progress_horizontal.xml文件,这个文件就是给progressDrawable用来指定进度的背景图片的.也是在android自带的源码上copy出来修改的.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source ProjectLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
--><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@android:id/background"android:drawable="@drawable/seekbar_background"/><item android:id="@android:id/secondaryProgress"><clip><shape><corners android:radius="5dip"/><gradientandroid:angle="270"android:centerColor="#80ffb600"android:centerY="0.75"android:endColor="#a0ffcb00"android:startColor="#80ffd300"/></shape></clip></item><item android:id="@android:id/progress"><clip><shape><corners android:radius="5dip"/><solid android:color="@color/volum_blue_bg"/></shape></clip></item></layer-list>

3.另外通过布局文件还可以知道,这里的VideoView是自定义的VideoView,为什么要自定义呢,因为android自带的VideoView在播放视频的时候没有提供全屏播放的功能,要实现全屏播放和按比例播放之间的切换就需要自定制.我这边是直接copy android自身的VideoView的源码,然后再其基础上添加全屏和按比例播放的功能.你可能会问,为什么不能直接在布局中设置match_parent的宽高属性来将VideoView全屏展示呢?这个就是因为VideoView源码中的onMeasure方法对宽高的设置进行了设置,布局上的match_parent并不能是VideoView全屏展示.

自定义的VideoView的代码如下:

/** Copyright (C) 2006 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package mchenys.net.csdn.blog.myplayer.common.widget;import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;import java.io.IOException;
import java.util.Map;import mchenys.net.csdn.blog.myplayer.common.utils.UIUtils;/*** Displays a video file.  The VideoView class* can load images from various sources (such as resources or content* providers), takes care of computing its measurement from the video so that* it can be used in any layout manager, and provides various display options* such as scaling and tinting.*/
public class VideoView extends SurfaceView implements MediaPlayerControl {private String TAG = "VideoView";// settable by the clientprivate Uri         mUri;private Map<String, String> mHeaders;// all possible internal statesprivate static final int STATE_ERROR              = -1;private static final int STATE_IDLE               = 0;private static final int STATE_PREPARING          = 1;private static final int STATE_PREPARED           = 2;private static final int STATE_PLAYING            = 3;private static final int STATE_PAUSED             = 4;private static final int STATE_PLAYBACK_COMPLETED = 5;// mCurrentState is a VideoView object's current state.// mTargetState is the state that a method caller intends to reach.// For instance, regardless the VideoView object's current state,// calling pause() intends to bring the object to a target state// of STATE_PAUSED.private int mCurrentState = STATE_IDLE;private int mTargetState  = STATE_IDLE;// All the stuff we need for playing and showing a videoprivate SurfaceHolder mSurfaceHolder = null;private MediaPlayer mMediaPlayer = null;private int         mAudioSession;private int         mVideoWidth;private int         mVideoHeight;private int         mSurfaceWidth;private int         mSurfaceHeight;private MediaController mMediaController;private OnCompletionListener mOnCompletionListener;private MediaPlayer.OnPreparedListener mOnPreparedListener;private int         mCurrentBufferPercentage;private OnErrorListener mOnErrorListener;private OnInfoListener  mOnInfoListener;private int         mSeekWhenPrepared;  // recording the seek position while preparingprivate boolean     mCanPause;private boolean     mCanSeekBack;private boolean     mCanSeekForward;private Context mContext;private int defaultWidth;private int defaultHeight;private int screenWidth;private int screenHeight;public VideoView(Context context) {super(context);initVideoView();}public VideoView(Context context, AttributeSet attrs) {this(context, attrs, 0);initVideoView();}public VideoView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initVideoView();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//Log.i("@@@@", "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", "//        + MeasureSpec.toString(heightMeasureSpec) + ")");int width = getDefaultSize(mVideoWidth, widthMeasureSpec);int height = getDefaultSize(mVideoHeight, heightMeasureSpec);if (mVideoWidth > 0 && mVideoHeight > 0) {int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);if (widthSpecMode == MeasureSpec.EXACTLY && heightSpecMode == MeasureSpec.EXACTLY) {// the size is fixedwidth = widthSpecSize;height = heightSpecSize;// for compatibility, we adjust size based on aspect ratioif ( 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;}} else if (widthSpecMode == MeasureSpec.EXACTLY) {// only the width is fixed, adjust the height to match aspect ratio if possiblewidth = widthSpecSize;height = width * mVideoHeight / mVideoWidth;if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {// couldn't match aspect ratio within the constraintsheight = heightSpecSize;}} else if (heightSpecMode == MeasureSpec.EXACTLY) {// only the height is fixed, adjust the width to match aspect ratio if possibleheight = heightSpecSize;width = height * mVideoWidth / mVideoHeight;if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {// couldn't match aspect ratio within the constraintswidth = widthSpecSize;}} else {// neither the width nor the height are fixed, try to use actual video sizewidth = mVideoWidth;height = mVideoHeight;if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {// too tall, decrease both width and heightheight = heightSpecSize;width = height * mVideoWidth / mVideoHeight;}if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {// too wide, decrease both width and heightwidth = widthSpecSize;height = width * mVideoHeight / mVideoWidth;}}} else {// no size yet, just adopt the given spec sizes}//这里为了获取videoView的默认宽高defaultWidth = width;defaultHeight = height;setMeasuredDimension(width, height);}@SuppressLint("NewApi") @Overridepublic void onInitializeAccessibilityEvent(AccessibilityEvent event) {super.onInitializeAccessibilityEvent(event);event.setClassName(VideoView.class.getName());}@SuppressLint("NewApi") @Overridepublic void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {super.onInitializeAccessibilityNodeInfo(info);info.setClassName(VideoView.class.getName());}public int resolveAdjustedSize(int desiredSize, int measureSpec) {return getDefaultSize(desiredSize, measureSpec);}private void initVideoView() {mContext = getContext();screenWidth = UIUtils.getScreenWidth();screenHeight = UIUtils.getScreenHeight();mVideoWidth = 0;mVideoHeight = 0;getHolder().addCallback(mSHCallback);getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);setFocusable(true);setFocusableInTouchMode(true);requestFocus();mCurrentState = STATE_IDLE;mTargetState  = STATE_IDLE;}public void setVideoPath(String path) {setVideoURI(Uri.parse(path));}public void setVideoURI(Uri uri) {setVideoURI(uri, null);}/*** @hide*/public void setVideoURI(Uri uri, Map<String, String> headers) {mUri = uri;mHeaders = headers;mSeekWhenPrepared = 0;openVideo();requestLayout();invalidate();}public void stopPlayback() {if (mMediaPlayer != null) {mMediaPlayer.stop();mMediaPlayer.release();mMediaPlayer = null;mCurrentState = STATE_IDLE;mTargetState  = STATE_IDLE;}}private void openVideo() {if (mUri == null || mSurfaceHolder == null) {// not ready for playback just yet, will try again laterreturn;}// Tell the music playback service to pause// TODO: these constants need to be published somewhere in the framework.Intent i = new Intent("com.android.music.musicservicecommand");i.putExtra("command", "pause");mContext.sendBroadcast(i);// we shouldn't clear the target state, because somebody might have// called start() previouslyrelease(false);try {mMediaPlayer = new MediaPlayer();
//            if (mAudioSession != 0) {
//                mMediaPlayer.setAudioSessionId(mAudioSession);
//            } else {mAudioSession = mMediaPlayer.getAudioSessionId();
//            }mMediaPlayer.setOnPreparedListener(mPreparedListener);mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);mMediaPlayer.setOnCompletionListener(mCompletionListener);mMediaPlayer.setOnErrorListener(mErrorListener);mMediaPlayer.setOnInfoListener(mOnInfoListener);mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);mCurrentBufferPercentage = 0;
//            mMediaPlayer.setDataSource(mContext, mUri, mHeaders);mMediaPlayer.setDataSource(mContext, mUri);mMediaPlayer.setDisplay(mSurfaceHolder);mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);mMediaPlayer.setScreenOnWhilePlaying(true);mMediaPlayer.prepareAsync();// we don't set the target state here either, but preserve the// target state that was there before.mCurrentState = STATE_PREPARING;attachMediaController();} catch (IOException ex) {Log.w(TAG, "Unable to open content: " + mUri, ex);mCurrentState = STATE_ERROR;mTargetState = STATE_ERROR;mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);return;} catch (IllegalArgumentException ex) {Log.w(TAG, "Unable to open content: " + mUri, ex);mCurrentState = STATE_ERROR;mTargetState = STATE_ERROR;mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);return;}}public void setMediaController(MediaController controller) {if (mMediaController != null) {mMediaController.hide();}mMediaController = controller;attachMediaController();}private void attachMediaController() {if (mMediaPlayer != null && mMediaController != null) {mMediaController.setMediaPlayer(this);View anchorView = this.getParent() instanceof View ?(View)this.getParent() : this;mMediaController.setAnchorView(anchorView);mMediaController.setEnabled(isInPlaybackState());}}MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener =new MediaPlayer.OnVideoSizeChangedListener() {public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {mVideoWidth = mp.getVideoWidth();mVideoHeight = mp.getVideoHeight();if (mVideoWidth != 0 && mVideoHeight != 0) {getHolder().setFixedSize(mVideoWidth, mVideoHeight);requestLayout();}}};MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {public void onPrepared(MediaPlayer mp) {mCurrentState = STATE_PREPARED;//这里注释掉,因为这部分判断是针对性能很差的机型做的判断.解开会爆红,也不影响使用
//             Get the capabilities of the player for this stream
//            Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL,
//                                      MediaPlayer.BYPASS_METADATA_FILTER);/* if (data != null) {mCanPause = !data.has(Metadata.PAUSE_AVAILABLE)|| data.getBoolean(Metadata.PAUSE_AVAILABLE);mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE)|| data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE);mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE)|| data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE);} else {*/mCanPause = mCanSeekBack = mCanSeekForward = true;
//            }if (mOnPreparedListener != null) {mOnPreparedListener.onPrepared(mMediaPlayer);}if (mMediaController != null) {mMediaController.setEnabled(true);}mVideoWidth = mp.getVideoWidth();mVideoHeight = mp.getVideoHeight();int seekToPosition = mSeekWhenPrepared;  // mSeekWhenPrepared may be changed after seekTo() callif (seekToPosition != 0) {seekTo(seekToPosition);}if (mVideoWidth != 0 && mVideoHeight != 0) {//Log.i("@@@@", "video size: " + mVideoWidth +"/"+ mVideoHeight);getHolder().setFixedSize(mVideoWidth, mVideoHeight);if (mSurfaceWidth == mVideoWidth && mSurfaceHeight == mVideoHeight) {// We didn't actually change the size (it was already at the size// we need), so we won't get a "surface changed" callback, so// start the video here instead of in the callback.if (mTargetState == STATE_PLAYING) {start();if (mMediaController != null) {mMediaController.show();}} else if (!isPlaying() &&(seekToPosition != 0 || getCurrentPosition() > 0)) {if (mMediaController != null) {// Show the media controls when we're paused into a video and make 'em stick.mMediaController.show(0);}}}} else {// We don't know the video size yet, but should start anyway.// The video size might be reported to us later.if (mTargetState == STATE_PLAYING) {start();}}}};private OnCompletionListener mCompletionListener =new OnCompletionListener() {public void onCompletion(MediaPlayer mp) {mCurrentState = STATE_PLAYBACK_COMPLETED;mTargetState = STATE_PLAYBACK_COMPLETED;if (mMediaController != null) {mMediaController.hide();}if (mOnCompletionListener != null) {mOnCompletionListener.onCompletion(mMediaPlayer);}}};private OnErrorListener mErrorListener =new OnErrorListener() {public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {Log.d(TAG, "Error: " + framework_err + "," + impl_err);mCurrentState = STATE_ERROR;mTargetState = STATE_ERROR;if (mMediaController != null) {mMediaController.hide();}/* If an error handler has been supplied, use it and finish. */if (mOnErrorListener != null) {if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) {return true;}}//下面注释的是报错时候弹出的对话框/* Otherwise, pop up an error dialog so the user knows that* something bad has happened. Only try and pop up the dialog* if we're attached to a window. When we're going away and no* longer have a window, don't bother showing the user an error.*//*if (getWindowToken() != null) {Resources r = mContext.getResources();int messageId;if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {messageId = com.android.internal.R.string.VideoView_error_text_invalid_progressive_playback;} else {messageId = com.android.internal.R.string.VideoView_error_text_unknown;}new AlertDialog.Builder(mContext).setMessage(messageId).setPositiveButton(com.android.internal.R.string.VideoView_error_button,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {If we get here, there is no onError listener, so* at least inform them that the video is over.if (mOnCompletionListener != null) {mOnCompletionListener.onCompletion(mMediaPlayer);}}}).setCancelable(false).show();}*/return true;}};private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener =new MediaPlayer.OnBufferingUpdateListener() {public void onBufferingUpdate(MediaPlayer mp, int percent) {mCurrentBufferPercentage = percent;}};public void setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener) {this.mBufferingUpdateListener = mBufferingUpdateListener;}/*** Register a callback to be invoked when the media file* is loaded and ready to go.** @param l The callback that will be run*/public void setOnPreparedListener(MediaPlayer.OnPreparedListener l){mOnPreparedListener = l;}/*** Register a callback to be invoked when the end of a media file* has been reached during playback.** @param l The callback that will be run*/public void setOnCompletionListener(OnCompletionListener l){mOnCompletionListener = l;}/*** Register a callback to be invoked when an error occurs* during playback or setup.  If no listener is specified,* or if the listener returned false, VideoView will inform* the user of any errors.** @param l The callback that will be run*/public void setOnErrorListener(OnErrorListener l){mOnErrorListener = l;}/*** Register a callback to be invoked when an informational event* occurs during playback or setup.** @param l The callback that will be run*/public void setOnInfoListener(OnInfoListener l) {mOnInfoListener = l;}SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback(){public void surfaceChanged(SurfaceHolder holder, int format,int w, int h){mSurfaceWidth = w;mSurfaceHeight = h;boolean isValidState =  (mTargetState == STATE_PLAYING);boolean hasValidSize = (mVideoWidth == w && mVideoHeight == h);if (mMediaPlayer != null && isValidState && hasValidSize) {if (mSeekWhenPrepared != 0) {seekTo(mSeekWhenPrepared);}start();}}public void surfaceCreated(SurfaceHolder holder){mSurfaceHolder = holder;openVideo();}public void surfaceDestroyed(SurfaceHolder holder){// after we return from this we can't use the surface any moremSurfaceHolder = null;if (mMediaController != null) mMediaController.hide();release(true);}};/** release the media player in any state*/private void release(boolean cleartargetstate) {if (mMediaPlayer != null) {mMediaPlayer.reset();mMediaPlayer.release();mMediaPlayer = null;mCurrentState = STATE_IDLE;if (cleartargetstate) {mTargetState  = STATE_IDLE;}}}@Overridepublic boolean onTouchEvent(MotionEvent ev) {if (isInPlaybackState() && mMediaController != null) {toggleMediaControlsVisiblity();}return false;}@Overridepublic boolean onTrackballEvent(MotionEvent ev) {if (isInPlaybackState() && mMediaController != null) {toggleMediaControlsVisiblity();}return false;}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event){boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK &&keyCode != KeyEvent.KEYCODE_VOLUME_UP &&keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &&keyCode != KeyEvent.KEYCODE_VOLUME_MUTE &&keyCode != KeyEvent.KEYCODE_MENU &&keyCode != KeyEvent.KEYCODE_CALL &&keyCode != KeyEvent.KEYCODE_ENDCALL;if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {if (mMediaPlayer.isPlaying()) {pause();mMediaController.show();} else {start();mMediaController.hide();}return true;} else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {if (!mMediaPlayer.isPlaying()) {start();mMediaController.hide();}return true;} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP|| keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {if (mMediaPlayer.isPlaying()) {pause();mMediaController.show();}return true;} else {toggleMediaControlsVisiblity();}}return super.onKeyDown(keyCode, event);}private void toggleMediaControlsVisiblity() {if (mMediaController.isShowing()) {mMediaController.hide();} else {mMediaController.show();}}@Overridepublic void start() {if (isInPlaybackState()) {mMediaPlayer.start();mCurrentState = STATE_PLAYING;}mTargetState = STATE_PLAYING;}@Overridepublic void pause() {if (isInPlaybackState()) {if (mMediaPlayer.isPlaying()) {mMediaPlayer.pause();mCurrentState = STATE_PAUSED;}}mTargetState = STATE_PAUSED;}public void suspend() {release(false);}public void resume() {openVideo();}@Overridepublic int getDuration() {if (isInPlaybackState()) {return mMediaPlayer.getDuration();}return -1;}@Overridepublic int getCurrentPosition() {if (isInPlaybackState()) {return mMediaPlayer.getCurrentPosition();}return 0;}@Overridepublic void seekTo(int msec) {if (isInPlaybackState()) {mMediaPlayer.seekTo(msec);mSeekWhenPrepared = 0;} else {mSeekWhenPrepared = msec;}}@Overridepublic boolean isPlaying() {return isInPlaybackState() && mMediaPlayer.isPlaying();}@Overridepublic int getBufferPercentage() {if (mMediaPlayer != null) {return mCurrentBufferPercentage;}return 0;}private boolean isInPlaybackState() {return (mMediaPlayer != null &&mCurrentState != STATE_ERROR &&mCurrentState != STATE_IDLE &&mCurrentState != STATE_PREPARING);}@Overridepublic boolean canPause() {return mCanPause;}@Overridepublic boolean canSeekBackward() {return mCanSeekBack;}@Overridepublic boolean canSeekForward() {return mCanSeekForward;}@Overridepublic int getAudioSessionId() {if (mAudioSession == 0) {MediaPlayer foo = new MediaPlayer();mAudioSession = foo.getAudioSessionId();foo.release();}return mAudioSession;}//以下就是对VideoView全屏切换的扩展private boolean isFullscreen;/** 在全屏和默认大小之间进行切换 */public void toggleFullscreen() {LayoutParams params = getLayoutParams();if (isFullscreen) {// 如果当前是全屏,则切换为默认大小params.width = defaultWidth;params.height = defaultHeight;} else {// 如果当前是默认大小,则切换为全屏params.width = screenWidth;params.height = screenHeight;}isFullscreen = !isFullscreen;requestLayout();    // 通知这个View去更新布局参数}/** 返回是否是全屏,true代表全屏 */public boolean isFullscreen() {return isFullscreen;}
}

使用Vitamio库打造万能播放器(一)相关推荐

  1. Vitamio库打造万能播放器(四)

    接着上一篇文章 Vitamio库打造万能播放器(三) 终于到了介绍vitamio的使用了,这个库使用起来非常简单,很容易上手,只需要将前面我们创建好的VideoPlayerActivity的Video ...

  2. Vitamio打造万能播放器使用步骤

    在电脑了,如果有一个.mp4文件 -高码率 拷贝到手机来,用系统自带的播放器播放.--基本码率LibVLC--MediaPlayerhttp://bbs.chinavideo.org/forumdis ...

  3. Android万能播放器Vitamio

    以下是一些参考博文,记录已下,方便以后使用 Android 如何直播RTMP流 农民伯伯-使用Vitamio打造万能播放器-1 农民伯伯-使用Vitamio打造万能播放器-2 一直更新到第9篇 农民伯 ...

  4. Vitamio打造自己的Android万能播放器

    前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的And ...

  5. 使用Vitamio打造自己的Android万能播放器(1)——准备

    前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的And ...

  6. 使用Vitamio打造自己的Android万能播放器

    前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的And ...

  7. 使用Vitamio打造自己的Android万能播放器(2)—— 手势控制亮度、音量、缩放

    前言 本章继续完善播放相关播放器的核心功能,为后续扩展打好基础. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over1 ...

  8. 使用Vitamio打造自己的Android万能播放器(7)——在线播放(下载视频)

    前言 本章将实现非常实用的功能--下载在线视频.涉及到多线程.线程更新UI等技术,还需思考产品的设计,如何将新加的功能更好的融入到现有的产品中,并不是简单的加一个界面就行了,欢迎大家交流产品设计和技术 ...

  9. [转载]使用Vitamio打造自己的Android万能播放器(7)——在线播放(下载视频)...

    前言 本章将实现非常实用的功能--下载在线视频.涉及到多线程.线程更新UI等技术,还需思考产品的设计,如何将新加的功能更好的融入到现有的产品中,并不是简单的加一个界面就行了,欢迎大家交流产品设计和技术 ...

最新文章

  1. 在读研、竞赛和实习中追求平衡
  2. golang 短连接和长连接 超时处理
  3. springmvc 传对象报400_那么火的SpringMVC到底有什么过人之处呢
  4. 学习过程中的一些想法
  5. 5分钟搞定AlertManager接入短信、语音等10+种通知渠道
  6. linux内核_Linux内核编程风格简介
  7. Keras-6 IMDB, a binary classification example
  8. 基于JAVA+SpringBoot+Mybatis+MYSQL的今日头条新闻网站
  9. 一个算例 介绍 libmesh 的类
  10. 安装 Dubbo 管理控制台
  11. React骚操作——jsx遇到template-directive
  12. 手把手分析 mfc 程序创建 代码执行流程
  13. 验证性因子分析中有哪些指标?
  14. 普通话测试软件字体怎么调整,普通话测试方案
  15. 系统安全性分析与设计
  16. ANSYS湿模态分析(一)_51CAE_新浪博客
  17. 全面了解风控决策引擎
  18. fabric.js 钢笔橡皮问题
  19. 减肥期间早餐或者晚餐吃馒头包子可以吗?
  20. To B 业务 vs To C 业务

热门文章

  1. PC微信机器人接口api之实战分析发送文件连续call
  2. 如何在React工程中使用JavaScript Barcode SDK创建Web条形码应用
  3. Global Game Jam 2018 推荐下载的37款免费资源
  4. 机器人点焊枪接线_Roboguide软件:机器人焊枪安装与焊接工件添加
  5. Xposed Java反射大法
  6. layer.open 弹窗 位置 修改 (layui)
  7. 【Unity3D】【NGUI】选中某个图标后高亮(或者灰化)
  8. 计算机图形学 1 —— 颜色模型图像基本知识,Phong光照模型
  9. HTML的字母H含义是,h字母含义
  10. 打字高手 7.6_打字稿3.9:发生了什么变化?