title: Unable to control volume and brightness of exoplayer
body: <p>With help of some question i managed to add volume and brightness control to my video player with <code>onTouchEvent</code> method. But when i swipe up or down my app close(it Restarts). Any help on my code why am not able to control volume and brightness using onTouchEvent.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>@Override
      public boolean onTouchEvent(MotionEvent event) {
          switch (event.getAction()){
              case MotionEvent.ACTION_DOWN:
                  tested_ok=false;
                  if (event.getX() &lt; (sWidth / 2)) {
                      intLeft = true;
                      intRight = false;
                  } else if (event.getX() &gt; (sWidth / 2)) {
                      intLeft = false;
                      intRight = true;
                  }
                  int upperLimit = (sHeight / 4) + 100;
                  int lowerLimit = ((sHeight / 4) * 3) - 150;
                  if (event.getY() &lt; upperLimit) {
                      intBottom = false;
                      intTop = true;
                  } else if (event.getY() &gt; 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) &gt; MIN_DISTANCE) {
                          tested_ok = true;
                      }
                      if (Math.abs(diffY) &gt; 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 &gt; 250) {
                                  new_brightness = 250;
                              } else if (new_brightness &lt; 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 &gt; maxVol) {
                                  newMediaVolume = maxVol;
                              } else if (newMediaVolume &lt; 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) &gt; Math.abs(diffY)) {
                          if (Math.abs(diffX) &gt; (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);
      }
  </code></pre>
  
  <p>FYI: i can't make Log since i don't have emulator. your help would be more helpful thanks in advance.</p>
  
  <p>Edit: when i swipe down on volume it move to zero, it doesn't reduce slowly and only the volume down works.</p>
  
  <p>Here is complete code of <code>VideoPlayerActivity.java</code></p>
  
  <pre><code>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() &lt; (sWidth / 2)) {
                      intLeft = true;
                      intRight = false;
                  } else if (event.getX() &gt; (sWidth / 2)) {
                      intLeft = false;
                      intRight = true;
                  }
                  int upperLimit = (sHeight / 4) + 100;
                  int lowerLimit = ((sHeight / 4) * 3) - 150;
                  if (event.getY() &lt; upperLimit) {
                      intBottom = false;
                      intTop = true;
                  } else if (event.getY() &gt; 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) &gt; MIN_DISTANCE) {
                          tested_ok = true;
                      }
                      if (Math.abs(diffY) &gt; 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 &gt; 250) {
                                  new_brightness = 250;
                              } else if (new_brightness &lt; 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 &lt; 30) {
                                  brightnessIcon.setImageResource(R.drawable.ic_bright_min);
                                  brightness_image.setImageResource(R.drawable.ic_bright_min);
                              } else if (brightPerc &gt; 30 &amp;&amp; brightPerc &lt; 80) {
                                  brightnessIcon.setImageResource(R.drawable.ic_bright_med);
                                  brightness_image.setImageResource(R.drawable.ic_bright_med);
                              } else if (brightPerc &gt; 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 &gt; maxVol) {
                                  newMediaVolume = maxVol;
                              } else if (newMediaVolume &lt; 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 &lt; 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 &gt;= 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) &gt; Math.abs(diffY)) {
                          if (Math.abs(diffX) &gt; (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);
      }
  
  }
  </code></pre>