[Android] [PATCH 3/3] Video: add volume control from keyboard
Alexandre Perraud
4leyx4ndre at gmail.com
Thu Sep 25 17:19:28 CEST 2014
and directly handle device volume buttons
---
.../vlc/gui/video/VideoPlayerActivity.java | 30 +++++++++++++++++++---
1 file changed, 26 insertions(+), 4 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 75cc98d..cce3607 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -208,6 +208,9 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
private OnAudioFocusChangeListener mAudioFocusListener;
private boolean mMute = false;
private int mVol, mVolSave;
+ private int mVolDelta = 0;
+ private static final int VOLUME_UP = 1;
+ private static final int VOLUME_DOWN = -1;
//Touch Events
private static final int TOUCH_NONE = 0;
@@ -405,6 +408,7 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
em.addHandler(eventHandler);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
+ mVol = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// Extra initialization when no secondary display is detected
if (mPresentation == null) {
@@ -703,6 +707,12 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
case KeyEvent.KEYCODE_VOLUME_MUTE:
updateMute();
return true;
+ case KeyEvent.KEYCODE_VOLUME_DOWN:
+ updateVolume(VOLUME_DOWN);
+ return true;
+ case KeyEvent.KEYCODE_VOLUME_UP:
+ updateVolume(VOLUME_UP);
+ return true;
case KeyEvent.KEYCODE_S:
case KeyEvent.KEYCODE_MEDIA_STOP:
finish();
@@ -1337,14 +1347,26 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
if (mTouchAction != TOUCH_NONE && mTouchAction != TOUCH_VOLUME)
return;
int delta = -(int) ((y_changed / mSurfaceYDisplayRange) * mAudioMax);
- int vol = (int) Math.min(Math.max(mVol + delta, 0), mAudioMax);
- if (delta != 0) {
- mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, vol, 0);
+ if (delta != 0 && mVolDelta != delta) {
+ if (delta > mVolDelta)
+ updateVolume(VOLUME_UP);
+ else
+ updateVolume(VOLUME_DOWN);
+ mVolDelta = delta;
mTouchAction = TOUCH_VOLUME;
- showInfo(getString(R.string.volume) + '\u00A0' + Integer.toString(vol),1000);
}
}
+ private void updateVolume (int delta) {
+ if (mMute) {
+ showInfo("Mute",1000);
+ return;
+ };
+ mVol = (int) Math.min(Math.max(mVol + delta, 0), mAudioMax);
+ mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mVol, 0);
+ showInfo(getString(R.string.volume) + '\u00A0' + Integer.toString(mVol),1000);
+ }
+
private void updateMute () {
if (!mMute) {
mVolSave = mVol;
--
1.9.1
More information about the Android
mailing list