手機影音6--視頻播放器的準系統(3),6--準系統

來源:互聯網
上載者:User

手機影音6--視頻播放器的準系統(3),6--準系統

1.自訂VideoView

1_自訂VideoView-增加設定視頻大小方法

public class VideoView extends android.widget.VideoView {    /**     * Android系統在更加xml布局找這個類,並且執行個體化的時候,用該構造方法執行個體化     *     * @param context     * @param attrs     */    public VideoView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public VideoView(Context context) {        super(context);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);    }    /**     * 設定視頻畫面的寬和高     * @param videoWidth     * @param videoHeight     */    public void setVideoSize(int videoWidth, int videoHeight) {        ViewGroup.LayoutParams layoutParams  =getLayoutParams();        layoutParams.width = videoWidth;        layoutParams.height = videoHeight;        setLayoutParams(layoutParams);    }}

2_得到螢幕高和寬方法

在播放器中

wm = (WindowManager) getSystemService(WINDOW_SERVICE);screenWidth = wm.getDefaultDisplay().getWidth();screenHeight = wm.getDefaultDisplay().getHeight();DisplayMetrics displayMetrics = new DisplayMetrics();this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);screenWidth = displayMetrics.widthPixels;screenHeight = displayMetrics.heightPixels;

3_視頻預設和全屏  

一播放起來,在準備好了中設定視頻播放預設

/** * true全屏 * flase預設*/private boolean isFullScreen = false;/** *  視頻全屏和預設 *  @param type*/public void setVideoType(int type){switch (type) {case SCREEN_FULL:videoview.setVideoSize(screenWidth, screenHeight);isFullScreen = true;btn_switch_screen.setBackgroundResource(R.drawable.btn_screen_dafult_selector);break;case SCREEN_DEFULT://視頻的寬int mVideoWidth = videoWidth;//視頻的高int mVideoHeight = videoHeight;//螢幕的寬int width = screenWidth;//螢幕的寬int height = screenHeight;if (mVideoWidth > 0 && mVideoHeight > 0) {if ( mVideoWidth * height  > width * mVideoHeight ) {//Log.i("@@@", "image too tall, correcting");height = width * mVideoHeight / mVideoWidth;} else if ( mVideoWidth * height  < width * mVideoHeight ) {//Log.i("@@@", "image too wide, correcting");width = height * mVideoWidth / mVideoHeight;} else {//Log.i("@@@", "aspect ratio is correct: " +//width+"/"+height+"="+//mVideoWidth+"/"+mVideoHeight);}}videoview.setVideoSize(width, height);btn_switch_screen.setBackgroundResource(R.drawable.btn_screen_full_selector);isFullScreen = false;break;    }}

4_螢幕保持不鎖屏

//設定螢幕不鎖屏getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

5_點擊按鈕的時候實現切換播放模式

case R.id.btn_switch_screen:if(isFullScreen){   setVideoType(SCREEN_DEFUALT);}else{   setVideoType(SCREEN_FULL);}break

  

  

  

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.