Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

Android之视频播放以及亮度音量变换

$
0
0

程序实现功能:
播放视频,右侧上下滑动改变亮度,左侧上下滑动改变音量。
现在我们来记住几句重要的属性
清单文件,强制横屏
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);

    }
}

下面是布局:

<?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"
    tools:context="com.edu.jereh.musicapp.VideoActivity"
    android:gravity="center"
    android:background="#000">
    <VideoView
        android:id="@+id/vv"
        android:foregroundGravity="center"
        android:keepScreenOn="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

视频截图

这里写图片描述

作者:woainijinying 发表于2016/9/26 22:57:12 原文链接
阅读:178 评论:1 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>