adding a seekbar to the panorama video view

This commit is contained in:
tibbi 2018-10-21 20:51:32 +02:00
parent 9fd3acd93f
commit 0cb212947c
3 changed files with 136 additions and 27 deletions

View file

@ -8,9 +8,13 @@ import android.os.Handler
import android.view.View import android.view.View
import android.view.Window import android.view.Window
import android.view.WindowManager import android.view.WindowManager
import android.view.animation.AnimationUtils
import android.widget.RelativeLayout import android.widget.RelativeLayout
import android.widget.SeekBar
import com.google.vr.sdk.widgets.video.VrVideoEventListener import com.google.vr.sdk.widgets.video.VrVideoEventListener
import com.google.vr.sdk.widgets.video.VrVideoView import com.google.vr.sdk.widgets.video.VrVideoView
import com.simplemobiletools.commons.extensions.getFormattedDuration
import com.simplemobiletools.commons.extensions.onGlobalLayout
import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
@ -24,15 +28,21 @@ import com.simplemobiletools.gallery.helpers.HIDE_PLAY_PAUSE_DELAY
import com.simplemobiletools.gallery.helpers.PATH import com.simplemobiletools.gallery.helpers.PATH
import com.simplemobiletools.gallery.helpers.PLAY_PAUSE_VISIBLE_ALPHA import com.simplemobiletools.gallery.helpers.PLAY_PAUSE_VISIBLE_ALPHA
import kotlinx.android.synthetic.main.activity_panorama_video.* import kotlinx.android.synthetic.main.activity_panorama_video.*
import kotlinx.android.synthetic.main.bottom_video_time_holder.*
import java.io.File import java.io.File
open class PanoramaVideoActivity : SimpleActivity() { open class PanoramaVideoActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListener {
private var isFullScreen = false private var mIsFullscreen = false
private var isExploreEnabled = true private var mIsExploreEnabled = true
private var isRendering = false private var mIsRendering = false
private var isPlaying = true private var mIsPlaying = false
private var mIsDragged = false
private var mPlayOnReady = false
private var mDuration = 0
private var mCurrTime = 0
private var mHidePlayPauseHandler = Handler() private var mHidePlayPauseHandler = Handler()
private var mTimerHandler = Handler()
public override fun onCreate(savedInstanceState: Bundle?) { public override fun onCreate(savedInstanceState: Bundle?) {
useDynamicTheme = false useDynamicTheme = false
@ -49,9 +59,9 @@ open class PanoramaVideoActivity : SimpleActivity() {
setupButtonMargins() setupButtonMargins()
explore.setOnClickListener { explore.setOnClickListener {
isExploreEnabled = !isExploreEnabled mIsExploreEnabled = !mIsExploreEnabled
vr_video_view.setPureTouchTracking(isExploreEnabled) vr_video_view.setPureTouchTracking(mIsExploreEnabled)
explore.setImageResource(if (isExploreEnabled) R.drawable.ic_explore else R.drawable.ic_explore_off) explore.setImageResource(if (mIsExploreEnabled) R.drawable.ic_explore else R.drawable.ic_explore_off)
} }
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {
@ -67,7 +77,7 @@ open class PanoramaVideoActivity : SimpleActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
vr_video_view.resumeRendering() vr_video_view.resumeRendering()
isRendering = true mIsRendering = true
if (config.blackBackground) { if (config.blackBackground) {
updateStatusbarColor(Color.BLACK) updateStatusbarColor(Color.BLACK)
} }
@ -78,17 +88,18 @@ open class PanoramaVideoActivity : SimpleActivity() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
vr_video_view.pauseRendering() vr_video_view.pauseRendering()
isRendering = false mIsRendering = false
} }
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
if (isRendering) { if (mIsRendering) {
vr_video_view.shutdown() vr_video_view.shutdown()
} }
if (!isChangingConfigurations) { if (!isChangingConfigurations) {
mHidePlayPauseHandler.removeCallbacksAndMessages(null) mHidePlayPauseHandler.removeCallbacksAndMessages(null)
mTimerHandler.removeCallbacksAndMessages(null)
} }
} }
@ -108,7 +119,8 @@ open class PanoramaVideoActivity : SimpleActivity() {
vr_video_view.apply { vr_video_view.apply {
loadVideo(Uri.fromFile(File(path)), options) loadVideo(Uri.fromFile(File(path)), options)
schedulePlayPauseFadeOut() pauseVideo()
setFlingingEnabled(true) setFlingingEnabled(true)
setPureTouchTracking(true) setPureTouchTracking(true)
@ -126,6 +138,22 @@ open class PanoramaVideoActivity : SimpleActivity() {
override fun onClick() { override fun onClick() {
handleClick() handleClick()
} }
override fun onLoadSuccess() {
if (mDuration == 0) {
setupDuration(duration)
setupTimer()
}
if (mPlayOnReady) {
mPlayOnReady = false
playVideo()
}
}
override fun onCompletion() {
videoCompleted()
}
}) })
} }
@ -137,7 +165,7 @@ open class PanoramaVideoActivity : SimpleActivity() {
} }
window.decorView.setOnSystemUiVisibilityChangeListener { visibility -> window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
isFullScreen = visibility and View.SYSTEM_UI_FLAG_FULLSCREEN != 0 mIsFullscreen = visibility and View.SYSTEM_UI_FLAG_FULLSCREEN != 0
toggleButtonVisibility() toggleButtonVisibility()
} }
} }
@ -147,19 +175,46 @@ open class PanoramaVideoActivity : SimpleActivity() {
setupButtonMargins() setupButtonMargins()
} }
private fun setupDuration(duration: Long) {
mDuration = (duration / 1000).toInt()
video_seekbar.max = mDuration
video_duration.text = mDuration.getFormattedDuration()
setVideoProgress(0)
}
private fun setupTimer() {
runOnUiThread(object : Runnable {
override fun run() {
if (mIsPlaying && !mIsDragged) {
mCurrTime = (vr_video_view!!.currentPosition / 1000).toInt()
video_seekbar.progress = mCurrTime
video_curr_time.text = mCurrTime.getFormattedDuration()
}
mTimerHandler.postDelayed(this, 1000)
}
})
}
private fun togglePlayPause() { private fun togglePlayPause() {
isPlaying = !isPlaying mIsPlaying = !mIsPlaying
mHidePlayPauseHandler.removeCallbacksAndMessages(null)
video_play_outline.alpha = PLAY_PAUSE_VISIBLE_ALPHA video_play_outline.alpha = PLAY_PAUSE_VISIBLE_ALPHA
schedulePlayPauseFadeOut() mHidePlayPauseHandler.removeCallbacksAndMessages(null)
if (isPlaying) { if (mIsPlaying) {
playVideo() playVideo()
} else { } else {
pauseVideo() pauseVideo()
} }
schedulePlayPauseFadeOut()
} }
private fun playVideo() { private fun playVideo() {
if (mCurrTime == mDuration) {
setVideoProgress(0)
mPlayOnReady = true
return
}
vr_video_view.playVideo() vr_video_view.playVideo()
video_play_outline.setImageResource(R.drawable.ic_pause) video_play_outline.setImageResource(R.drawable.ic_pause)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
@ -171,6 +226,22 @@ open class PanoramaVideoActivity : SimpleActivity() {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} }
private fun setVideoProgress(seconds: Int) {
vr_video_view.seekTo(seconds * 1000L)
video_seekbar.progress = seconds
mCurrTime = seconds
video_curr_time.text = seconds.getFormattedDuration()
}
private fun videoCompleted() {
mIsPlaying = false
mCurrTime = (vr_video_view.duration / 1000).toInt()
video_seekbar.progress = video_seekbar.max
video_curr_time.text = mDuration.getFormattedDuration()
pauseVideo()
video_play_outline.alpha = PLAY_PAUSE_VISIBLE_ALPHA
}
private fun schedulePlayPauseFadeOut() { private fun schedulePlayPauseFadeOut() {
mHidePlayPauseHandler.removeCallbacksAndMessages(null) mHidePlayPauseHandler.removeCallbacksAndMessages(null)
mHidePlayPauseHandler.postDelayed({ mHidePlayPauseHandler.postDelayed({
@ -179,21 +250,57 @@ open class PanoramaVideoActivity : SimpleActivity() {
} }
private fun setupButtonMargins() { private fun setupButtonMargins() {
(explore.layoutParams as RelativeLayout.LayoutParams).bottomMargin = navigationBarHeight (video_time_holder.layoutParams as RelativeLayout.LayoutParams).bottomMargin = navigationBarHeight
video_time_holder.onGlobalLayout {
(explore.layoutParams as RelativeLayout.LayoutParams).bottomMargin = navigationBarHeight + video_time_holder.height
explore.requestLayout()
}
} }
private fun toggleButtonVisibility() { private fun toggleButtonVisibility() {
explore.animate().alpha(if (isFullScreen) 0f else 1f) explore.animate().alpha(if (mIsFullscreen) 0f else 1f)
explore.isClickable = !isFullScreen explore.isClickable = !mIsFullscreen
var anim = android.R.anim.fade_in
if (mIsFullscreen) {
anim = android.R.anim.fade_out
video_seekbar.setOnSeekBarChangeListener(null)
} else {
video_seekbar.setOnSeekBarChangeListener(this)
}
AnimationUtils.loadAnimation(this, anim).apply {
duration = 150
fillAfter = true
video_time_holder.startAnimation(this)
}
} }
private fun handleClick() { private fun handleClick() {
isFullScreen = !isFullScreen mIsFullscreen = !mIsFullscreen
toggleButtonVisibility() toggleButtonVisibility()
if (isFullScreen) { if (mIsFullscreen) {
hideSystemUI(false) hideSystemUI(false)
} else { } else {
showSystemUI(false) showSystemUI(false)
} }
} }
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser) {
setVideoProgress(progress)
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
vr_video_view.pauseVideo()
mIsDragged = true
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
mIsPlaying = true
playVideo()
mIsDragged = false
schedulePlayPauseFadeOut()
}
} }

View file

@ -85,6 +85,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
mTimeHolder = video_time_holder mTimeHolder = video_time_holder
mBrightnessSideScroll = video_brightness_controller mBrightnessSideScroll = video_brightness_controller
mVolumeSideScroll = video_volume_controller mVolumeSideScroll = video_volume_controller
mCurrTimeView = video_curr_time
} }
storeStateVariables() storeStateVariables()
@ -317,7 +318,6 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
mTimeHolder.setPadding(left, top, right, bottom) mTimeHolder.setPadding(left, top, right, bottom)
mCurrTimeView = mView!!.video_curr_time
mSeekBar = mView!!.video_seekbar mSeekBar = mView!!.video_seekbar
mSeekBar!!.setOnSeekBarChangeListener(this) mSeekBar!!.setOnSeekBarChangeListener(this)
mTimeHolder.beInvisibleIf(mIsFullscreen) mTimeHolder.beInvisibleIf(mIsFullscreen)
@ -604,10 +604,10 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
if (mExoPlayer == null) if (mExoPlayer == null)
return return
if (!mIsPlaying) { if (mIsPlaying) {
togglePlayPause()
} else {
mExoPlayer!!.playWhenReady = true mExoPlayer!!.playWhenReady = true
} else {
togglePlayPause()
} }
mIsDragged = false mIsDragged = false

View file

@ -27,6 +27,8 @@
android:layout_centerInParent="true" android:layout_centerInParent="true"
android:background="@drawable/circle_black_background_with_inset" android:background="@drawable/circle_black_background_with_inset"
android:padding="26dp" android:padding="26dp"
android:src="@drawable/ic_pause"/> android:src="@drawable/ic_play"/>
<include layout="@layout/bottom_video_time_holder"/>
</RelativeLayout> </RelativeLayout>