fix some video playback issues on Android 7

This commit is contained in:
tibbi 2017-10-23 21:30:24 +02:00
parent 012e091349
commit baed126550
2 changed files with 19 additions and 16 deletions

View file

@ -742,9 +742,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun getCurrentFile() = File(getCurrentPath()) private fun getCurrentFile() = File(getCurrentPath())
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
}
override fun onPageSelected(position: Int) { override fun onPageSelected(position: Int) {
if (view_pager.offscreenPageLimit == 1) { if (view_pager.offscreenPageLimit == 1) {

View file

@ -20,7 +20,6 @@ import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.MEDIUM import com.simplemobiletools.gallery.helpers.MEDIUM
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.pager_video_item.view.* import kotlinx.android.synthetic.main.pager_video_item.view.*
import java.io.IOException
class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener { class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener {
private val CLICK_MAX_DURATION = 150 private val CLICK_MAX_DURATION = 150
@ -40,6 +39,7 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
private var mIsFragmentVisible = false private var mIsFragmentVisible = false
private var mPlayOnPrepare = false private var mPlayOnPrepare = false
private var mStoredShowExtendedDetails = false private var mStoredShowExtendedDetails = false
private var wasInit = false
private var mStoredExtendedDetails = 0 private var mStoredExtendedDetails = 0
private var mCurrTime = 0 private var mCurrTime = 0
private var mDuration = 0 private var mDuration = 0
@ -71,6 +71,7 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
mIsFullscreen = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN == View.SYSTEM_UI_FLAG_FULLSCREEN mIsFullscreen = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN == View.SYSTEM_UI_FLAG_FULLSCREEN
checkFullscreen() checkFullscreen()
wasInit = true
return mView return mView
} }
@ -124,21 +125,21 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
initTimeHolder() initTimeHolder()
checkExtendedDetails() checkExtendedDetails()
initMediaPlayer()
} }
override fun setMenuVisibility(menuVisible: Boolean) { override fun setMenuVisibility(menuVisible: Boolean) {
super.setMenuVisibility(menuVisible) super.setMenuVisibility(menuVisible)
if (mIsFragmentVisible && !menuVisible) {
pauseVideo()
releaseMediaPlayer()
}
mIsFragmentVisible = menuVisible mIsFragmentVisible = menuVisible
if (menuVisible) { if (menuVisible && wasInit) {
if (mSurfaceView != null && mSurfaceHolder!!.surface.isValid) { initMediaPlayer()
initMediaPlayer()
}
if (context?.config?.autoplayVideos == true) { if (context?.config?.autoplayVideos == true) {
playVideo() playVideo()
} }
} else if (mIsPlaying) {
pauseVideo()
} }
} }
@ -351,6 +352,8 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
if (activity == null || !isAdded) if (activity == null || !isAdded)
return return
initMediaPlayer()
mIsPlaying = !mIsPlaying mIsPlaying = !mIsPlaying
if (mIsPlaying) { if (mIsPlaying) {
playVideo() playVideo()
@ -378,8 +381,9 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
} }
private fun initMediaPlayer() { private fun initMediaPlayer() {
if (mMediaPlayer != null) if (mMediaPlayer != null || !mIsFragmentVisible) {
return return
}
try { try {
mMediaPlayer = MediaPlayer().apply { mMediaPlayer = MediaPlayer().apply {
@ -389,9 +393,9 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
setOnVideoSizeChangedListener({ mediaPlayer, width, height -> setVideoSize() }) setOnVideoSizeChangedListener({ mediaPlayer, width, height -> setVideoSize() })
setOnPreparedListener { videoPrepared(it) } setOnPreparedListener { videoPrepared(it) }
setAudioStreamType(AudioManager.STREAM_MUSIC) setAudioStreamType(AudioManager.STREAM_MUSIC)
prepareAsync() prepare()
} }
} catch (e: IOException) { } catch (e: Exception) {
releaseMediaPlayer() releaseMediaPlayer()
} }
} }
@ -441,7 +445,9 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
} }
override fun surfaceCreated(holder: SurfaceHolder) { override fun surfaceCreated(holder: SurfaceHolder) {
mSurfaceHolder = holder
if (mIsFragmentVisible)
initMediaPlayer()
} }
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
@ -462,7 +468,6 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
initMediaPlayer() initMediaPlayer()
if (mMediaPlayer == null) { if (mMediaPlayer == null) {
activity.toast(R.string.unknown_error_occurred)
return return
} }