Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android之視頻播放以及亮度音量變換

Android之視頻播放以及亮度音量變換

編輯:關於Android編程

程序實現功能:
播放視頻,右側上下滑動改變亮度,左側上下滑動改變音量。
現在我們來記住幾句重要的屬性
清單文件,強制橫屏
android:screenOrientation=”landscape”
不會重新執行Oncreat
android:configChanges=”orientation|keyboard”
android:configChanges=”orientation|keyboard|screenSize
它會走onConfigurationChanged
VideoView中:android:foregroundGravity=”center”
下面是代碼啦

public class VideoActivity extends Activity {
private VideoView vv;
    private int k;
    //手勢
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_video);
        vv = (VideoView) findViewById(R.id.vv);
        MediaController mediaController=new MediaController(this);
        vv.setMediaController(mediaController);
        mediaController.setMediaPlayer(vv);
        Intent intent =getIntent();
        String url=intent.getStringExtra("playurl");
        vv.setVideoPath(url);
        vv.requestFocus();
        vv.start();
        //返回上一頁
        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                finish();
            }
        });
        //據說並不好用,需要和onTouchEvent配合使用
        setGestureDetector();
    }
//觸摸(觸摸事件)
    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        //手指抬起
//        if(event.getAction()==MotionEvent.ACTION_UP){
//            Log.d("==g==","up");
//        }
//        //手指按下
//        if(event.getAction() == MotionEvent.ACTION_DOWN){
//            Log.d("==g==","down");
//            Log.d("==g==", "down" + event.getX());
//            Log.d("==g==", "down" + event.getY());
//
//        }
//        //手指移動
//        if(event.getAction() == MotionEvent.ACTION_MOVE){
//            Log.d("==g==","move");
//        }
//類的監聽,完成解析
        return gestureDetector.onTouchEvent(event);
    }
    //手勢方法
    public void setGestureDetector(){
        gestureDetector =new GestureDetector(
                new GestureDetector.OnGestureListener() {
                    @Override
                    public boolean onDown(MotionEvent e) {
                        return false;
                    }

                    @Override
                    public void onShowPress(MotionEvent e) {

                    }

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        return false;
                    }
//滾動MotionEvent  e1 1.手勢起點事件
// MotionEvent e2 2.手勢終點
//distanceX 3.x軸方向上移動的速度/每秒
// distanceY 4.Y軸方向上移動的速度/每秒

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                        if(e1.getY()-e2.getY()>0.5&&Math.abs(distanceY)>0.5){
                           if(e1.getX()>500){
                               setLightness(30);
                           } else{
                               setAudio(1);
                           }
                        }
                        if(e1.getY()-e2.getY()<0.5&&Math.abs(distanceY)>0.5){
                            Log.d("==g==", e1.getY() + "");
                            if(e1.getX()>500){
                                setLightness(-30);
                            }else{
                                setAudio(-1);
                            }
                        }
                        return true;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {

                    }
//拋的動作
                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        return false;
                    }
                }
        );
    }
//物理鍵按下
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }
//物理鍵抬起
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    //軌跡球
    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        return super.onTrackballEvent(event);
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Log.d("==d==", ""+newConfig.orientation);
    }
//改變屏幕亮度
    public void setLightness(float lightness){
        WindowManager.LayoutParams layoutParams =getWindow().getAttributes();
        //屏幕的亮度,最大是255
        layoutParams.screenBrightness =layoutParams.screenBrightness+lightness/255f;
        if(layoutParams.screenBrightness>1){
            layoutParams.screenBrightness=1;
        }else if(layoutParams.screenBrightness<0.2){
            layoutParams.screenBrightness=0.2f;
        }
        getWindow().setAttributes(layoutParams);
    }
//加減音量
    public void setAudio(int volume){
        AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        //當前音量
       k = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        //最大音量
        int max =audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        Log.d("==d==", "" + max);
        Log.d("==d==", "" + k);
        k=k+volume;
        if(k>=0&&k<=max){
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,k,AudioManager.FLAG_PLAY_SOUND);
        }else {
            return;
        }


        //audioManager.adjustVolume(i+volume,AudioManager.FLAG_PLAY_SOUND);

    }
}

下面是布局:



    


視頻截圖

這裡寫圖片描述

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved