无法控制exoplayer的音量和亮度

Unable to control volume and brightness of exoplayer

在一些问题的帮助下,我设法使用 onTouchEvent 方法为我的视频播放器添加了音量和亮度控制。但是当我向上或向下滑动我的应用程序时关闭(它重新启动)。关于我的代码的任何帮助,为什么我无法使用 onTouchEvent 控制音量和亮度。

这是我的代码:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                tested_ok=false;
                if (event.getX() < (sWidth / 2)) {
                    intLeft = true;
                    intRight = false;
                } else if (event.getX() > (sWidth / 2)) {
                    intLeft = false;
                    intRight = true;
                }
                int upperLimit = (sHeight / 4) + 100;
                int lowerLimit = ((sHeight / 4) * 3) - 150;
                if (event.getY() < upperLimit) {
                    intBottom = false;
                    intTop = true;
                } else if (event.getY() > lowerLimit) {
                    intBottom = true;
                    intTop = false;
                } else {
                    intBottom = false;
                    intTop = false;
                }
                seekSpeed = (TimeUnit.MILLISECONDS.toSeconds(exoPlayer.getDuration()) * 0.1);
                diffX = 0;
                calculatedTime = 0;
                seekDur = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(diffX) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(diffX)),
                TimeUnit.MILLISECONDS.toSeconds(diffX) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(diffX)));

                //TOUCH STARTED
                baseX = event.getX();
                baseY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                screen_swipe_move=true;
                    root.setVisibility(View.GONE);
                    diffX = (long) (Math.ceil(event.getX() - baseX));
                    diffY = (long) Math.ceil(event.getY() - baseY);
                    double brightnessSpeed = 0.05;
                    if (Math.abs(diffY) > MIN_DISTANCE) {
                        tested_ok = true;
                    }
                    if (Math.abs(diffY) > Math.abs(diffX)) {
                        if (intLeft) {
                            cResolver = getContentResolver();
                            window = getWindow();
                            try {
                                Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                                brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
                            } catch (Settings.SettingNotFoundException e) {
                                e.printStackTrace();
                            }
                            int new_brightness = (int) (brightness - (diffY * brightnessSpeed));
                            if (new_brightness > 250) {
                                new_brightness = 250;
                            } else if (new_brightness < 1) {
                                new_brightness = 1;
                            }
                            double brightPerc = Math.ceil((((double) new_brightness / (double) 250) * (double) 100));
                            brightnessBarContainer.setVisibility(View.VISIBLE);
                            brightness_center_text.setVisibility(View.VISIBLE);
                            brightnessBar.setProgress((int) brightPerc);

                            brigtness_perc_center_text.setText(" " + (int) brightPerc);
                            Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, (new_brightness));
                            WindowManager.LayoutParams layoutpars = window.getAttributes();
                            layoutpars.screenBrightness = brightness / (float) 255;
                            window.setAttributes(layoutpars);
                        }else if (intRight) {
                            vol_center_text.setVisibility(View.VISIBLE);
                            mediavolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                            int maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                            double cal = (double) diffY * ((double)maxVol/(double)(device_height*4));
                            int newMediaVolume = mediavolume - (int) cal;
                            if (newMediaVolume > maxVol) {
                                newMediaVolume = maxVol;
                            } else if (newMediaVolume < 1) {
                                newMediaVolume = 0;
                            }
                            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newMediaVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                            double volPerc = Math.ceil((((double) newMediaVolume / (double) maxVol) * (double) 100));
                            vol_perc_center_text.setText(" " + (int) volPerc);

                            volumeBarContainer.setVisibility(View.VISIBLE);
                            volumeBar.setProgress((int) volPerc);
                        }
                    }else if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > (MIN_DISTANCE + 100)) {

                        }
                    }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                screen_swipe_move=false;
                tested_ok = false;

                seekBar_center_text.setVisibility(View.GONE);
                brightness_center_text.setVisibility(View.GONE);
                vol_center_text.setVisibility(View.GONE);
                brightnessBarContainer.setVisibility(View.GONE);
                volumeBarContainer.setVisibility(View.GONE);
                onlySeekbar.setVisibility(View.VISIBLE);
                root.setVisibility(View.VISIBLE);
                calculatedTime = (int) (exoPlayer.getCurrentPosition() + (calculatedTime));
                exoPlayer.seekTo(calculatedTime);
                break;

        }
        return super.onTouchEvent(event);
    }

仅供参考:我无法创建日志,因为我没有模拟器。提前谢谢你的帮助。

编辑:当我向下滑动音量时,它会移动到零,它不会慢慢减小,只有降低音量才有效。

这里是VideoPlayerActivity.java

的完整代码
package com.sanoj.jlplayer.Activities;



public class VideoPlayerActivity extends AppCompatActivity {

    Uri videoUri;
    @BindView(R.id.playerView) PlayerView playerView;
    @BindView(R.id.floating_widget) ImageView imageView;
    @BindView(R.id.exoFullscreenIcon) ImageView imageView1;
    @BindView(R.id.vol_perc_center_text) TextView vol_perc_center_text;
    @BindView(R.id.brigtness_perc_center_text) TextView brigtness_perc_center_text;
    @BindView(R.id.volume_slider) ProgressBar volumeBar;
    @BindView(R.id.brightness_slider) ProgressBar brightnessBar;
    @BindView(R.id.volume_slider_container) LinearLayout volumeBarContainer;
    @BindView(R.id.brightness_slider_container) LinearLayout brightnessBarContainer;
    @BindView(R.id.brightness_center_text) LinearLayout brightness_center_text;
    @BindView(R.id.vol_center_text) LinearLayout vol_center_text;
    @BindView(R.id.volIcon) ImageView volIcon;
    @BindView(R.id.brightnessIcon) ImageView brightnessIcon;
    @BindView(R.id.vol_image) ImageView vol_image;
    @BindView(R.id.brightness_image) ImageView brightness_image;

    boolean fullscreen = false;

    ExoPlayer exoPlayer;
    ExtractorsFactory extractorsFactory;

    int sWidth,sHeight;
    float baseX, baseY;
    long diffX, diffY;
    int calculatedTime;
    String seekDur;
    Boolean tested_ok = false;
    Boolean screen_swipe_move = false;
    boolean immersiveMode, intLeft, intRight, intTop, intBottom, finLeft, finRight, finTop, finBottom;
    static final int MIN_DISTANCE = 150;
    ContentResolver cResolver;
    Window window;
    TextView txt_seek_secs,txt_seek_currTime;
    int brightness, mediavolume,device_height,device_width;
    AudioManager audioManager;
    double seekSpeed = 0;
    LinearLayout root, seekBar_center_text, onlySeekbar;
    Point size;
    Display display;

    SeekBar seekBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);

        ButterKnife.bind(this);
        Intent intent = getIntent();

        //audio and brightness


        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        display = getWindowManager().getDefaultDisplay();
        size = new Point();
        display.getSize(size);
        sWidth = size.x;
        sHeight = size.y;

        if (intent!=null){
            String uri_str = intent.getStringExtra("videoUri");
            videoUri = Uri.parse(uri_str);
        }

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exoPlayer.setPlayWhenReady(false);
                exoPlayer.release();
                Intent serviceIntent = new Intent(VideoPlayerActivity.this,FloatingWidgetService.class);
                serviceIntent.putExtra("videoUri",videoUri.toString());
                startService(serviceIntent);
            }
        });


        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

        TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
        exoPlayer = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
        extractorsFactory = new DefaultExtractorsFactory();
        playVideo();

    }


    private void playVideo() {
        try {
            String playerInfo = Util.getUserAgent(this,"VideoPlayer");
            DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,playerInfo);

            MediaSource mediaSource = new ExtractorMediaSource(videoUri,dataSourceFactory,extractorsFactory,null,null);

            playerView.setPlayer(exoPlayer);
            exoPlayer.prepare(mediaSource);
            exoPlayer.setPlayWhenReady(true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        exoPlayer.setPlayWhenReady(false);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exoPlayer.setPlayWhenReady(false);
        exoPlayer.release();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                tested_ok=false;
                if (event.getX() < (sWidth / 2)) {
                    intLeft = true;
                    intRight = false;
                } else if (event.getX() > (sWidth / 2)) {
                    intLeft = false;
                    intRight = true;
                }
                int upperLimit = (sHeight / 4) + 100;
                int lowerLimit = ((sHeight / 4) * 3) - 150;
                if (event.getY() < upperLimit) {
                    intBottom = false;
                    intTop = true;
                } else if (event.getY() > lowerLimit) {
                    intBottom = true;
                    intTop = false;
                } else {
                    intBottom = false;
                    intTop = false;
                }
                seekSpeed = (TimeUnit.MILLISECONDS.toSeconds(exoPlayer.getDuration()) * 0.1);
                diffX = 0;
                calculatedTime = 0;
                seekDur = String.format("%02d:%02d",
                        TimeUnit.MILLISECONDS.toMinutes(diffX) -
                                TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(diffX)),
                        TimeUnit.MILLISECONDS.toSeconds(diffX) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(diffX)));

                //TOUCH STARTED
                baseX = event.getX();
                baseY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                screen_swipe_move=true;
                    root.setVisibility(View.GONE);
                    diffX = (long) (Math.ceil(event.getX() - baseX));
                    diffY = (long) Math.ceil(event.getY() - baseY);
                    double brightnessSpeed = 0.05;
                    if (Math.abs(diffY) > MIN_DISTANCE) {
                        tested_ok = true;
                    }
                    if (Math.abs(diffY) > Math.abs(diffX)) {
                        if (intLeft) {
                            cResolver = getContentResolver();
                            window = getWindow();
                            try {
                                Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                                brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
                            } catch (Settings.SettingNotFoundException e) {
                                e.printStackTrace();
                            }
                            int new_brightness = (int) (brightness - (diffY * brightnessSpeed));
                            if (new_brightness > 250) {
                                new_brightness = 250;
                            } else if (new_brightness < 1) {
                                new_brightness = 1;
                            }
                            double brightPerc = Math.ceil((((double) new_brightness / (double) 250) * (double) 100));
                            brightnessBarContainer.setVisibility(View.VISIBLE);
                            brightness_center_text.setVisibility(View.VISIBLE);
                            brightnessBar.setProgress((int) brightPerc);
                            if (brightPerc < 30) {
                                brightnessIcon.setImageResource(R.drawable.ic_bright_min);
                                brightness_image.setImageResource(R.drawable.ic_bright_min);
                            } else if (brightPerc > 30 && brightPerc < 80) {
                                brightnessIcon.setImageResource(R.drawable.ic_bright_med);
                                brightness_image.setImageResource(R.drawable.ic_bright_med);
                            } else if (brightPerc > 80) {
                                brightnessIcon.setImageResource(R.drawable.ic_bright_max);
                                brightness_image.setImageResource(R.drawable.ic_bright_max);
                            }
                            brigtness_perc_center_text.setText(" " + (int) brightPerc);
                            Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, (new_brightness));
                            WindowManager.LayoutParams layoutpars = window.getAttributes();
                            layoutpars.screenBrightness = brightness / (float) 255;
                            window.setAttributes(layoutpars);
                        }else if (intRight) {
                            vol_center_text.setVisibility(View.VISIBLE);
                            mediavolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                            int maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                            double cal = (double) diffY * ((double)maxVol/(double)(device_height*4));
                            int newMediaVolume = mediavolume - (int) cal;
                            if (newMediaVolume > maxVol) {
                                newMediaVolume = maxVol;
                            } else if (newMediaVolume < 1) {
                                newMediaVolume = 0;
                            }
                            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newMediaVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                            double volPerc = Math.ceil((((double) newMediaVolume / (double) maxVol) * (double) 100));
                            vol_perc_center_text.setText(" " + (int) volPerc);
                            if (volPerc < 1) {
                                volIcon.setImageResource(R.drawable.ic_volume_mute);
                                vol_image.setImageResource(R.drawable.ic_volume_mute);
                                vol_perc_center_text.setVisibility(View.GONE);
                            } else if (volPerc >= 1) {
                                volIcon.setImageResource(R.drawable.ic_volume);
                                vol_image.setImageResource(R.drawable.ic_volume);
                                vol_perc_center_text.setVisibility(View.VISIBLE);
                            }
                            volumeBarContainer.setVisibility(View.VISIBLE);
                            volumeBar.setProgress((int) volPerc);
                        }
                    }else if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > (MIN_DISTANCE + 100)) {
                            tested_ok = true;
                            root.setVisibility(View.VISIBLE);
                            seekBar_center_text.setVisibility(View.VISIBLE);
                            onlySeekbar.setVisibility(View.VISIBLE);
                            (exoPlayer.getCurrentPosition() + (calculatedTime)));
                        }
                    }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                screen_swipe_move=false;
                tested_ok = false;

                seekBar_center_text.setVisibility(View.GONE);
                brightness_center_text.setVisibility(View.GONE);
                vol_center_text.setVisibility(View.GONE);
                brightnessBarContainer.setVisibility(View.GONE);
                volumeBarContainer.setVisibility(View.GONE);
                onlySeekbar.setVisibility(View.VISIBLE);
                root.setVisibility(View.VISIBLE);
                calculatedTime = (int) (exoPlayer.getCurrentPosition() + (calculatedTime));
                exoPlayer.seekTo(calculatedTime);
                break;

        }
        return super.onTouchEvent(event);
    }

}

为了通过触摸控制亮度和音量,我设法找到了 https://github.com/moneytoo/Player,它让我可以使用更多功能来完成该功能,并使用了 exoplayer

和 exoplayer 一样易于构建。