From af84b8e9e3cba9e124aa596d7dab2420a77ad0b0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 6 Aug 2017 18:35:28 +0200 Subject: [PATCH] allow changing the volume by finger dragging on the right side of the video --- .../simplemobiletools/gallery/extensions/context.kt | 2 ++ .../gallery/fragments/VideoFragment.kt | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt index ebbf2772f..2d990287a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.Intent import android.content.res.Configuration import android.database.Cursor +import android.media.AudioManager import android.net.Uri import android.provider.MediaStore import com.simplemobiletools.commons.extensions.* @@ -21,6 +22,7 @@ import java.io.File import java.util.* val Context.portrait get() = resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT +val Context.audioManager get() = getSystemService(Context.AUDIO_SERVICE) as AudioManager fun Context.getRealPathFromURI(uri: Uri): String? { var cursor: Cursor? = null diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt index a2e2202b9..a816ee4bb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt @@ -18,6 +18,7 @@ import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.ViewPagerActivity +import com.simplemobiletools.gallery.extensions.audioManager import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.getNavBarHeight import com.simplemobiletools.gallery.extensions.hasNavBar @@ -26,6 +27,7 @@ import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.pager_video_item.view.* import java.io.IOException + class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener { private val CLICK_MAX_DURATION = 150 @@ -48,6 +50,7 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee private var mTouchDownX = 0f private var mTouchDownY = 0f private var mTouchDownTime = 0L + private var mTouchDownVolume = 0 lateinit var mView: View lateinit var medium: Medium @@ -128,6 +131,7 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee mTouchDownX = event.x mTouchDownY = event.y mTouchDownTime = System.currentTimeMillis() + mTouchDownVolume = getCurrentVolume() } MotionEvent.ACTION_MOVE -> { val diffX = mTouchDownX - event.x @@ -147,8 +151,15 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee } } - private fun volumePercentChanged(percent: Int) { + private fun getCurrentVolume() = context.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + private fun volumePercentChanged(percent: Int) { + val stream = AudioManager.STREAM_MUSIC + val maxVolume = context.audioManager.getStreamMaxVolume(stream) + val percentPerPoint = Math.ceil(100 / maxVolume.toDouble()).toInt() + val addPoints = percent / percentPerPoint + val newVolume = Math.min(maxVolume, Math.max(0, mTouchDownVolume + addPoints)) + context.audioManager.setStreamVolume(stream, newVolume, 0) } private fun initTimeHolder() {