init exoplayer only if play is pressed
This commit is contained in:
parent
c71593e90f
commit
ec20d67fff
2 changed files with 89 additions and 59 deletions
|
@ -5,6 +5,7 @@ import android.content.res.Configuration
|
||||||
import android.graphics.Point
|
import android.graphics.Point
|
||||||
import android.graphics.SurfaceTexture
|
import android.graphics.SurfaceTexture
|
||||||
import android.media.AudioManager
|
import android.media.AudioManager
|
||||||
|
import android.media.MediaMetadataRetriever
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
@ -14,6 +15,7 @@ import android.view.*
|
||||||
import android.view.animation.AnimationUtils
|
import android.view.animation.AnimationUtils
|
||||||
import android.widget.SeekBar
|
import android.widget.SeekBar
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import com.bumptech.glide.Glide
|
||||||
import com.google.android.exoplayer2.*
|
import com.google.android.exoplayer2.*
|
||||||
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
|
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
|
||||||
import com.google.android.exoplayer2.source.ExtractorMediaSource
|
import com.google.android.exoplayer2.source.ExtractorMediaSource
|
||||||
|
@ -110,56 +112,26 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
|
||||||
|
|
||||||
video_curr_time.setOnClickListener { skip(false) }
|
video_curr_time.setOnClickListener { skip(false) }
|
||||||
video_duration.setOnClickListener { skip(true) }
|
video_duration.setOnClickListener { skip(true) }
|
||||||
|
Glide.with(context!!).load(medium.path).into(video_preview)
|
||||||
}
|
}
|
||||||
|
|
||||||
mExoPlayer = ExoPlayerFactory.newSimpleInstance(context, DefaultTrackSelector())
|
mExoPlayer = ExoPlayerFactory.newSimpleInstance(context, DefaultTrackSelector())
|
||||||
mExoPlayer!!.addListener(object : Player.EventListener {
|
initExoPlayerListeners()
|
||||||
override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters?) {}
|
|
||||||
|
|
||||||
override fun onSeekProcessed() {}
|
|
||||||
|
|
||||||
override fun onTracksChanged(trackGroups: TrackGroupArray?, trackSelections: TrackSelectionArray?) {}
|
|
||||||
|
|
||||||
override fun onPlayerError(error: ExoPlaybackException?) {
|
|
||||||
mIsExoPlayerInitialized = false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onLoadingChanged(isLoading: Boolean) {}
|
|
||||||
|
|
||||||
override fun onPositionDiscontinuity(reason: Int) {}
|
|
||||||
|
|
||||||
override fun onRepeatModeChanged(repeatMode: Int) {}
|
|
||||||
|
|
||||||
override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) {}
|
|
||||||
|
|
||||||
override fun onTimelineChanged(timeline: Timeline?, manifest: Any?, reason: Int) {}
|
|
||||||
|
|
||||||
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
|
|
||||||
mIsExoPlayerInitialized = playbackState == Player.STATE_READY || playbackState == Player.STATE_ENDED
|
|
||||||
when (playbackState) {
|
|
||||||
Player.STATE_READY -> videoPrepared()
|
|
||||||
Player.STATE_ENDED -> videoCompleted()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
mExoPlayer!!.addVideoListener(object : VideoListener {
|
|
||||||
override fun onVideoSizeChanged(width: Int, height: Int, unappliedRotationDegrees: Int, pixelWidthHeightRatio: Float) {
|
|
||||||
mVideoSize.x = width
|
|
||||||
mVideoSize.y = height
|
|
||||||
setVideoSize()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onRenderedFirstFrame() {}
|
|
||||||
})
|
|
||||||
|
|
||||||
initExoPlayer()
|
|
||||||
medium.path.getVideoResolution()?.apply {
|
medium.path.getVideoResolution()?.apply {
|
||||||
mVideoSize.x = x
|
mVideoSize.x = x
|
||||||
mVideoSize.y = y
|
mVideoSize.y = y
|
||||||
setVideoSize()
|
setVideoSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setupVideoDuration()
|
||||||
|
|
||||||
|
mView!!.video_surface.onGlobalLayout {
|
||||||
|
if (mIsFragmentVisible && context?.config?.autoplayVideos == true) {
|
||||||
|
playVideo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return mView
|
return mView
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,6 +174,25 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun setMenuVisibility(menuVisible: Boolean) {
|
||||||
|
super.setMenuVisibility(menuVisible)
|
||||||
|
if (mIsFragmentVisible && !menuVisible) {
|
||||||
|
pauseVideo()
|
||||||
|
}
|
||||||
|
|
||||||
|
mIsFragmentVisible = menuVisible
|
||||||
|
if (mWasFragmentInit && menuVisible && context?.config?.autoplayVideos == true) {
|
||||||
|
playVideo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||||
|
super.onConfigurationChanged(newConfig)
|
||||||
|
setVideoSize()
|
||||||
|
initTimeHolder()
|
||||||
|
checkExtendedDetails()
|
||||||
|
}
|
||||||
|
|
||||||
private fun storeStateVariables() {
|
private fun storeStateVariables() {
|
||||||
context!!.config.apply {
|
context!!.config.apply {
|
||||||
mStoredShowExtendedDetails = showExtendedDetails
|
mStoredShowExtendedDetails = showExtendedDetails
|
||||||
|
@ -243,29 +234,46 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
|
||||||
mExoPlayer!!.prepare(audioSource)
|
mExoPlayer!!.prepare(audioSource)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setMenuVisibility(menuVisible: Boolean) {
|
private fun initExoPlayerListeners() {
|
||||||
super.setMenuVisibility(menuVisible)
|
mExoPlayer!!.addListener(object : Player.EventListener {
|
||||||
if (mIsFragmentVisible && !menuVisible) {
|
override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters?) {}
|
||||||
pauseVideo()
|
|
||||||
|
override fun onSeekProcessed() {}
|
||||||
|
|
||||||
|
override fun onTracksChanged(trackGroups: TrackGroupArray?, trackSelections: TrackSelectionArray?) {}
|
||||||
|
|
||||||
|
override fun onPlayerError(error: ExoPlaybackException?) {
|
||||||
|
mIsExoPlayerInitialized = false
|
||||||
}
|
}
|
||||||
|
|
||||||
mIsFragmentVisible = menuVisible
|
override fun onLoadingChanged(isLoading: Boolean) {}
|
||||||
if (menuVisible && mWasFragmentInit) {
|
|
||||||
if (!mIsExoPlayerInitialized && mExoPlayer != null) {
|
|
||||||
initExoPlayer()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context?.config?.autoplayVideos == true) {
|
override fun onPositionDiscontinuity(reason: Int) {}
|
||||||
playVideo()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
override fun onRepeatModeChanged(repeatMode: Int) {}
|
||||||
super.onConfigurationChanged(newConfig)
|
|
||||||
|
override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) {}
|
||||||
|
|
||||||
|
override fun onTimelineChanged(timeline: Timeline?, manifest: Any?, reason: Int) {}
|
||||||
|
|
||||||
|
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
|
||||||
|
mIsExoPlayerInitialized = playbackState == Player.STATE_READY || playbackState == Player.STATE_ENDED
|
||||||
|
when (playbackState) {
|
||||||
|
Player.STATE_READY -> videoPrepared()
|
||||||
|
Player.STATE_ENDED -> videoCompleted()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
mExoPlayer!!.addVideoListener(object : VideoListener {
|
||||||
|
override fun onVideoSizeChanged(width: Int, height: Int, unappliedRotationDegrees: Int, pixelWidthHeightRatio: Float) {
|
||||||
|
mVideoSize.x = width
|
||||||
|
mVideoSize.y = height
|
||||||
setVideoSize()
|
setVideoSize()
|
||||||
initTimeHolder()
|
}
|
||||||
checkExtendedDetails()
|
|
||||||
|
override fun onRenderedFirstFrame() {}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun toggleFullscreen() {
|
private fun toggleFullscreen() {
|
||||||
|
@ -390,6 +398,11 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mView!!.video_preview.isVisible()) {
|
||||||
|
mView!!.video_preview.beGone()
|
||||||
|
initExoPlayer()
|
||||||
|
}
|
||||||
|
|
||||||
if (videoEnded()) {
|
if (videoEnded()) {
|
||||||
setProgress(0)
|
setProgress(0)
|
||||||
}
|
}
|
||||||
|
@ -426,6 +439,18 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
|
||||||
mCurrTimeView!!.text = seconds.getFormattedDuration()
|
mCurrTimeView!!.text = seconds.getFormattedDuration()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun setupVideoDuration() {
|
||||||
|
try {
|
||||||
|
val retriever = MediaMetadataRetriever()
|
||||||
|
retriever.setDataSource(medium.path)
|
||||||
|
mDuration = Math.round(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toInt() / 1000f)
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
}
|
||||||
|
|
||||||
|
setupTimeHolder()
|
||||||
|
setProgress(0)
|
||||||
|
}
|
||||||
|
|
||||||
private fun videoPrepared() {
|
private fun videoPrepared() {
|
||||||
if (mDuration == 0) {
|
if (mDuration == 0) {
|
||||||
mDuration = (mExoPlayer!!.duration / 1000).toInt()
|
mDuration = (mExoPlayer!!.duration / 1000).toInt()
|
||||||
|
|
|
@ -6,6 +6,11 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/video_preview"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"/>
|
||||||
|
|
||||||
<TextureView
|
<TextureView
|
||||||
android:id="@+id/video_surface"
|
android:id="@+id/video_surface"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
|
|
Loading…
Reference in a new issue