Android自定義視訊播放器(六)
阿新 • • 發佈:2019-01-29
一、引言
現在已經差不多完成了一個自定義的視訊播放器,還有一個讓視訊全屏播放的按鈕事件沒有繫結,下面會介紹。此外還要一個非常重要的功能就是,已經寫好了自定義的播放器,那麼就應該能被其他應用調起,來播放視訊,下面來完善這兩個功能。
二、全屏按鈕事件的繫結
1、呼叫設定全屏方法
private void setFullScreenAndDefault() { if (isFullScreen) { //預設 setVideoType(DEFAULT_SCREEN); } else { //全屏 setVideoType(FULL_SCREEN); } }
2、setVideoType()方法
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; case DEFAULT_SCREEN://預設 //1.設定視訊畫面的大小 //視訊真實的寬和高 int mVideoWidth = videoWidth; int mVideoHeight = videoHeight; //螢幕的寬和高 int width = screenWidth; int height = screenHeight; if (mVideoWidth * height < width * mVideoHeight) { width = height * mVideoWidth / mVideoHeight; } else if (mVideoWidth * height > width * mVideoHeight) { height = width * mVideoHeight / mVideoWidth; } videoview.setVideoSize(width, height); //2.設定按鈕的狀態--全屏 btnVideoSiwchScreen.setBackgroundResource(R.drawable.btn_video_siwch_screen_full_selector); isFullScreen = false; break; } }
3、當視訊Prepared後獲取視訊的長和寬
videoWidth = mp.getVideoWidth();
videoHeight = mp.getVideoHeight();
4、獲取手機螢幕的長和寬
//得到螢幕的寬和高 DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); screenWidth = displayMetrics.widthPixels; screenHeight = displayMetrics.heightPixels;
5、說明
此處除了按鈕的點選事件,還可以實現雙擊螢幕,就可以實現螢幕大小的切換,方法的呼叫,放在自定義的MySimpleOnGestureListener 中onDoubleTapEvent()中方法即可。
三、讓第三方應用調起
1、使用隱式意圖
<activity android:name=".activity.VideoActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="rtsp" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*" />
<data android:mimeType="application/sdp" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:mimeType="video/mp4" />
<data android:mimeType="video/3gp" />
<data android:mimeType="video/3gpp" />
<data android:mimeType="video/3gpp2" />
</intent-filter>
</activity>
2、調起方,呼叫方法
//1.調起系統所有的播放-隱式意圖
Intent intent = new Intent();
intent.setDataAndType(Uri.parse("視訊播放地址"),"video/*");
context.startActivity(intent);
3、視訊播放器就會被調起並且播放
uri = getIntent().getData();
此處在前文中,介紹過一點,就是傳遞過來的結果如果是集合的情況和其他第三方應用的情況。留單獨獲取一個uri的情況就是在這裡出現。
4、設定播放地址
videoview.setVideoURI(uri);