Skip to content

Commit

Permalink
fix resize mode issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Amuzadeh committed May 1, 2021
1 parent 2a2942d commit 0f906b5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import com.potyvideo.library.kotlin.AndExoPlayerView
import com.potyvideo.library.kotlin.globalEnums.EnumResizeMode
import com.potyvideo.library.kotlin.globalInterfaces.AndExoPlayerListener
import com.potyvideo.library.kotlin.utils.PublicValues
import com.potyvideo.library.utils.PathUtil
Expand All @@ -25,7 +26,7 @@ class MainActivityKotlin : AppCompatActivity(), AndExoPlayerListener, View.OnCli

// andExoPlayerView.setSource(videoURL)
// andExoPlayerView.setShowTimeOut(0) // disable hide controller
andExoPlayerView.setResizeMode() // sync with attrs
andExoPlayerView.setResizeMode(EnumResizeMode.ZOOM) // sync with attrs
andExoPlayerView.setAndExoPlayerListener(this)

findViewById<AppCompatButton>(R.id.local).setOnClickListener(this)
Expand All @@ -34,9 +35,6 @@ class MainActivityKotlin : AppCompatActivity(), AndExoPlayerListener, View.OnCli
findViewById<AppCompatButton>(R.id.stream_dash).setOnClickListener(this)
findViewById<AppCompatButton>(R.id.stream_mkv).setOnClickListener(this)

// player controller view model
// val playerControllerViewModel = ViewModelProvider(this@MainActivityKotlin)[PlayerControllerViewModel::class.java]
// andExoPlayerView.setPlayerControllerViewModel(playerControllerViewModel)
}

override fun onExoPlayerError(errorMessage: String?) {
Expand All @@ -48,7 +46,7 @@ class MainActivityKotlin : AppCompatActivity(), AndExoPlayerListener, View.OnCli
selectLocaleVideo()
}
R.id.stream_mp4 -> {
loadMP4Stream(PublicValues.TEST_URL_MP4_V4)
loadMP4Stream(PublicValues.TEST_URL_MP4_V5)
}
R.id.stream_hls -> {
loadHLSStream(PublicValues.TEST_URL_HLS)
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 18
versionName "2.0.2"
versionCode 19
versionName "2.0.3"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,11 @@ abstract class AndExoPlayerRoot @JvmOverloads constructor(
unMute = playerView.findViewById(R.id.exo_unmute)
settingContainer = playerView.findViewById(R.id.container_setting)

// extract attrs
extractAttrs(attributeSet)

// listeners
initListeners()

}

private fun extractAttrs(attributeSet: AttributeSet?) {

attributeSet.let {
val attributes: TypedArray = context.obtainStyledAttributes(it, R.styleable.AndExoPlayerView)
if (attributes.hasValue(R.styleable.AndExoPlayerView_andexo_aspect_ratio)) {
val aspectRatio = attributes.getInteger(R.styleable.AndExoPlayerView_andexo_aspect_ratio, EnumAspectRatio.ASPECT_16_9.value)
setAspectRatio(EnumAspectRatio[aspectRatio])
}

attributes.recycle()
}

/*
val attributes = context.obtainStyledAttributes(attrs, R.styleable.BenefitView)
imageView.setImageDrawable(attributes.getDrawable(R.styleable.BenefitView_image))
textView.text = attributes.getString(R.styleable.BenefitView_text)
attributes.recycle()
*/
}

private fun initListeners() {
retryButton.setOnClickListener(customClickListener)
backwardView.setOnClickListener(customClickListener)
Expand Down Expand Up @@ -136,40 +113,6 @@ abstract class AndExoPlayerRoot @JvmOverloads constructor(
unMute.visibility = GONE
}

protected fun setAspectRatio(aspectRatio: EnumAspectRatio) {
this.currAspectRatio = aspectRatio
val value = PublicFunctions.getScreenWidth()
when (aspectRatio) {
EnumAspectRatio.ASPECT_1_1 -> playerView.layoutParams = FrameLayout.LayoutParams(value, value)
EnumAspectRatio.ASPECT_4_3 -> playerView.layoutParams = FrameLayout.LayoutParams(value, 3 * value / 4)
EnumAspectRatio.ASPECT_16_9 -> playerView.layoutParams = FrameLayout.LayoutParams(value, 9 * value / 16)
EnumAspectRatio.ASPECT_MATCH -> playerView.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
EnumAspectRatio.ASPECT_MP3 -> {
playerView.controllerShowTimeoutMs = 0
playerView.controllerHideOnTouch = false
val mp3Height = context.resources.getDimensionPixelSize(R.dimen.player_controller_base_height)
playerView.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mp3Height)
}
EnumAspectRatio.UNDEFINE -> {
val baseHeight = resources.getDimension(R.dimen.player_base_height).toInt()
playerView.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, baseHeight)
}
}
}

protected fun setRepeatMode(repeatMode: EnumRepeatMode) {
this.currRepeatMode = repeatMode
}

protected fun setResizeMode(resizeMode: EnumResizeMode) {
when (resizeMode) {
EnumResizeMode.FIT -> playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
EnumResizeMode.FILL -> playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL
EnumResizeMode.ZOOM -> playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
else -> playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
}
}

protected fun setShowSetting(showSetting: Boolean = false) {
if (showSetting)
settingContainer.visibility = VISIBLE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.potyvideo.library.kotlin

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.source.TrackGroupArray
import com.google.android.exoplayer2.trackselection.TrackSelectionArray
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.util.MimeTypes
import com.potyvideo.library.R
import com.potyvideo.library.kotlin.globalEnums.EnumAspectRatio
import com.potyvideo.library.kotlin.globalEnums.EnumRepeatMode
import com.potyvideo.library.kotlin.globalEnums.EnumResizeMode
import com.potyvideo.library.kotlin.globalInterfaces.AndExoPlayerListener
import com.potyvideo.library.kotlin.utils.DoubleClick
import com.potyvideo.library.kotlin.utils.DoubleClickListener
Expand Down Expand Up @@ -60,6 +66,28 @@ class AndExoPlayerView(

init {
player.addListener(this)

extractAttrs(attributeSet)
}

private fun extractAttrs(attributeSet: AttributeSet?) {

attributeSet.let {
val attributes: TypedArray = context.obtainStyledAttributes(it, R.styleable.AndExoPlayerView)
if (attributes.hasValue(R.styleable.AndExoPlayerView_andexo_aspect_ratio)) {
val aspectRatio = attributes.getInteger(R.styleable.AndExoPlayerView_andexo_aspect_ratio, EnumAspectRatio.ASPECT_16_9.value)
setAspectRatio(EnumAspectRatio[aspectRatio])
}

attributes.recycle()
}

/*
val attributes = context.obtainStyledAttributes(attrs, R.styleable.BenefitView)
imageView.setImageDrawable(attributes.getDrawable(R.styleable.BenefitView_image))
textView.text = attributes.getString(R.styleable.BenefitView_text)
attributes.recycle()
*/
}

override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters) {
Expand Down Expand Up @@ -186,10 +214,6 @@ class AndExoPlayerView(
playerView.controllerHideOnTouch = false
}

fun setResizeMode() {
playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL
}

fun seekBackward(backwardValue: Int = 10000) {
var seekValue = player.currentPosition - backwardValue
if (seekValue < 0) seekValue = 0
Expand Down Expand Up @@ -217,7 +241,7 @@ class AndExoPlayerView(
showUnMuteButton()
}

fun setLoopMode(repeatModeMode: EnumRepeatMode = EnumRepeatMode.REPEAT_OFF) {
fun setRepeatMode(repeatModeMode: EnumRepeatMode = EnumRepeatMode.REPEAT_OFF) {
when (repeatModeMode) {
EnumRepeatMode.REPEAT_OFF -> {
player.repeatMode = Player.REPEAT_MODE_OFF
Expand All @@ -231,4 +255,34 @@ class AndExoPlayerView(
}
}

fun setAspectRatio(aspectRatio: EnumAspectRatio) {
this.currAspectRatio = aspectRatio
val value = com.potyvideo.library.utils.PublicFunctions.getScreenWidth()
when (aspectRatio) {
EnumAspectRatio.ASPECT_1_1 -> playerView.layoutParams = FrameLayout.LayoutParams(value, value)
EnumAspectRatio.ASPECT_4_3 -> playerView.layoutParams = FrameLayout.LayoutParams(value, 3 * value / 4)
EnumAspectRatio.ASPECT_16_9 -> playerView.layoutParams = FrameLayout.LayoutParams(value, 9 * value / 16)
EnumAspectRatio.ASPECT_MATCH -> playerView.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
EnumAspectRatio.ASPECT_MP3 -> {
playerView.controllerShowTimeoutMs = 0
playerView.controllerHideOnTouch = false
val mp3Height = context.resources.getDimensionPixelSize(R.dimen.player_controller_base_height)
playerView.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mp3Height)
}
EnumAspectRatio.UNDEFINE -> {
val baseHeight = resources.getDimension(R.dimen.player_base_height).toInt()
playerView.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, baseHeight)
}
}
}

fun setResizeMode(resizeMode: EnumResizeMode) {
when (resizeMode) {
EnumResizeMode.FIT -> playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
EnumResizeMode.FILL -> playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL
EnumResizeMode.ZOOM -> playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
else -> playerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class PublicValues {
const val TEST_URL_MP4_V2 = "https://hajifirouz2.cdn.asset.aparat.com/aparat-video/069670345914165d063b0738fc9f81b532132940-480p.mp4?wmsAuthSign=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbiI6ImMxMjg4ZDQzM2NhNzZhZmRjNGM1M2E3NTQyMjIwNTQwIiwiZXhwIjoxNjE4MzQ1NzA4LCJpc3MiOiJTYWJhIElkZWEgR1NJRyJ9.TbxCUzv7ke-TuYsxekYjN3VVnpG48uWOQ-7iN_ZH3_g"
const val TEST_URL_MP4_V3 = "https://www.rmp-streaming.com/media/big-buck-bunny-360p.mp4"
const val TEST_URL_MP4_V4 = "https://backyard.vaslapp.com/res/036e77f0-a94e-4165-9832-1a8e39c1e089/USER_FILES/6084024b90c61d0001471b0c/1619507956737/file.mp4"
const val TEST_URL_MP4_V5 = "https://backyard.vaslapp.com/res/036e77f0-a94e-4165-9832-1a8e39c1e089/USER_FILES/6084024b90c61d0001471b0c/1619507956737/file.mp4"

const val TEST_URL_HLS = "https://content.jwplatform.com/manifests/yp34SRmf.m3u8"

Expand Down

0 comments on commit 0f906b5

Please sign in to comment.