Android本地文件点击视频播放器vitamio版
生活随笔
收集整理的這篇文章主要介紹了
Android本地文件点击视频播放器vitamio版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本博客用Android studio集成了vitamio的自定義視頻播放器,同時可以在文件管理中選擇播放,想要一個自己的視頻播放器這邊便可以滿足
實現具體功能如下:
1.sd卡內存視頻文件的點擊播放;
2.視頻時間進度條;
3.屏幕亮度的手勢滑動;
4.視頻音量的手勢滑動;
5.多點手勢效果
6.自定義存儲視頻url播放
想要的重要的代碼,我懂得:
------------------------------軟件工程結構圖:
最重要的是依賴上vitamio庫:
-------------------------工具類FileUtil,檢查文件格式是否屬于自己定義的格式:
-------------實體類屏幕的寬高ScreenBean,保存視頻寬高:
public class ScreenBean {private int sWidth;private int sHeight;public ScreenBean(int sWidth, int sHeight) {super();this.sWidth = sWidth;this.sHeight = sHeight;}public int getsWidth() {return sWidth;}public void setsWidth(int sWidth) {this.sWidth = sWidth;}public int getsHeight() {return sHeight;}public void setsHeight(int sHeight) {this.sHeight = sHeight;} } ----------------------中轉傳遞界面MainActivity類:
public class MainActivity extends AppCompatActivity {String Path;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent getPath=getIntent();String action = getPath.getAction();if(getPath.ACTION_VIEW.equals(action)){Path=getPath.getDataString();}else {Path= Environment.getExternalStorageDirectory() + "/test.mp4";}Intent intent = new Intent(this, VideoPlay.class);intent.putExtra("videoURL",Path);startActivity(intent);finish();} }這個界面是中轉,開發時一般自己定義,
Path=getPath.getDataString();//獲取sd卡的視頻文件url路徑 Path= Environment.getExternalStorageDirectory() + "/test.mp4"; //程序存儲的路徑,可以掃描手機內存視頻存儲到數據庫,這種視頻列表簡單自己寫.
----------------------視頻播放Activity類:
public class VideoPlay extends Activity {/** 當前視頻路徑 */private String path;/** 當前聲音 */private int mVolume = -1;/** 最大音量 */private int mMaxVolume;/** 當前亮度 */private float mBrightness = -1f;/** 手勢數目 */private int finNum=0;private View mVolumeBrightnessLayout;private ImageView mOperationBg;private ImageView mOperationPercent;private VideoView mVideoView;private GestureDetector gestDetector;private ScaleGestureDetector scaleDetector;private ScreenBean screenBean;@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);if (!LibsChecker.checkVitamioLibs(this))return;requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_video);Intent getPath=getIntent();path=getPath.getStringExtra("videoURL");init();}private void init() {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);mMaxVolume = LocUtil.getMaxVolume(this);gestDetector = new GestureDetector(this, new SingleGestureListener());scaleDetector = new ScaleGestureDetector(this,new MultiGestureListener());screenBean = LocUtil.getScreenPix(this);if (path == "") {return;} else {mVideoView.setVideoPath(path);mVideoView.setMediaController(new MediaController(this));mVideoView.requestFocus();mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {@Overridepublic void onPrepared(MediaPlayer mediaPlayer) {mediaPlayer.setPlaybackSpeed(1.0f);}});}}/** 定時隱藏 */private Handler mDismissHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {mVolumeBrightnessLayout.setVisibility(View.GONE);}};@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubfinNum=event.getPointerCount();if (1 == finNum) {gestDetector.onTouchEvent(event);switch (event.getAction()) {case MotionEvent.ACTION_UP:endGesture();}} else if (2 ==finNum) {scaleDetector.onTouchEvent(event);}return true;}/** 手勢結束 */private void endGesture() {mVolume = -1;mBrightness = -1f;// 隱藏mDismissHandler.removeMessages(0);mDismissHandler.sendEmptyMessageDelayed(0, 500);}/*** 視頻縮放*/public void changeLayout(int size) {mVideoView.setVideoLayout(size, 0);}/*** 聲音大小**/public void changeVolume(float percent) {if (mVolume == -1) {mVolume = LocUtil.getCurVolume(this);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;// 變更聲音LocUtil.setCurVolume(this, index);// 變更進度條ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();lp.width = findViewById(R.id.operation_full).getLayoutParams().width* index / mMaxVolume;mOperationPercent.setLayoutParams(lp);}/*** 亮度大小* * @param percent*/public void changeBrightness(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);}/*** 單點觸屏*/private class SingleGestureListener implementsGestureDetector.OnGestureListener {@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubLog.d("Fling", velocityY);return false;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {// TODO Auto-generated method stubif(2==finNum){return false;}float moldX = e1.getX();float moldY = e1.getY();float y = e2.getY();if (moldX > screenBean.getsWidth() * 9.0 / 10)// 右邊滑動changeVolume((moldY - y) / screenBean.getsHeight());else if (moldX < screenBean.getsWidth() / 10.0)// 左邊滑動changeBrightness((moldY - y) / screenBean.getsHeight());return false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}}//多點手勢控制private class MultiGestureListener implements OnScaleGestureListener {@Overridepublic boolean onScale(ScaleGestureDetector detector) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onScaleBegin(ScaleGestureDetector detector) {// TODO Auto-generated method stub// 返回true ,才能進入onscale()函數return true;}@Overridepublic void onScaleEnd(ScaleGestureDetector detector) {// TODO Auto-generated method stubfloat oldDis = detector.getPreviousSpan();float curDis = detector.getCurrentSpan();if (oldDis - curDis > 50) {// 縮小changeLayout(0);Toast.makeText(VideoPlay.this, "縮小", Toast.LENGTH_LONG).show();} else if (oldDis - curDis < -50) {// 放大changeLayout(1);Toast.makeText(VideoPlay.this, "放大", Toast.LENGTH_LONG).show();}}} }------------視頻布局activity_video(activity_main里面沒寫省略):<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" ><io.vov.vitamio.widget.VideoViewandroid:id="@+id/surface_view"android:layout_centerInParent="true"android:layout_width="wrap_content"android:layout_height="wrap_content" /><FrameLayoutandroid:id="@+id/operation_volume_brightness"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="#00000000"android:orientation="horizontal"android:padding="0dip"android:visibility="invisible" ><ImageViewandroid:id="@+id/operation_bg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:src="@drawable/video_volumn_bg" /><FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|center_horizontal"android:paddingBottom="25dip" ><ImageViewandroid:id="@+id/operation_full"android:layout_width="94dip"android:layout_height="wrap_content"android:layout_gravity="left"android:src="@drawable/video_num_bg" /><ImageViewandroid:id="@+id/operation_percent"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_gravity="left"android:scaleType="matrix"android:src="@drawable/video_num_front" /></FrameLayout></FrameLayout></RelativeLayout>-------------Androidmanifest清單文件(4個注意點):
1.權限:
<uses-sdkandroid:minSdkVersion="7"android:targetSdkVersion="23" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2.注意初始化vitamio的InitActivity:
<!-- Don't forgot InitActivity --><activityandroid:name="io.vov.vitamio.activity.InitActivity"android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"android:launchMode="singleTop"android:theme="@android:style/Theme.NoTitleBar"android:windowSoftInputMode="stateAlwaysHidden" />3.添加intent隱式意圖,對sd卡支持的文件格式添加本程序的打開方式:
<activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter><intent-filter><action android:name="android.intent.action.VIEW"></action><category android:name="android.intent.category.DEFAULT"></category><data android:mimeType="video/*"/><data android:mimeType="application/mp4"/><data android:mimeType="*/rmvb"/><data android:mimeType="*/avi"/><data android:mimeType="*/mkv"/><data android:mimeType="audio/x-pn-realaudio"/><data android:mimeType="video/x-ms-asf"/><data android:mimeType="video/quicktime"/><data android:mimeType="application/mpeg*"/><data android:mimeType="application/vnd.rn-realmedia*"/><data android:mimeType="application/3gpp*"/><data android:mimeType="application/vnd.3gp*"/><data android:mimeType="application/vnd.dvb*"/><data android:mimeType="application/vnd.dolby*"/><data android:mimeType="application/octet-stream"/></intent-filter></activity>4.添加視頻播放控制的Activity:
<activityandroid:name=".VideoPlay"android:screenOrientation="landscape" ></activity>
-------庫和圖片資源路徑:
個人網盤鏈接:http://pan.baidu.com/s/1jHSoZSM 密碼:03hm
視頻項目完整代碼或者編寫出問題的可以給我評論留言,我后面會解答
總結
以上是生活随笔為你收集整理的Android本地文件点击视频播放器vitamio版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站数据库分类
- 下一篇: 观察者模式案例的简单分析