Skip to content

Commit

Permalink
feat(player): add an API to get/set how the view is displayed in its …
Browse files Browse the repository at this point in the history
…parent view
  • Loading branch information
ThibaultBee committed Oct 27, 2023
1 parent 655d58c commit b7fa735
Showing 1 changed file with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package video.api.player.views

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.ui.AspectRatioFrameLayout.RESIZE_MODE_FILL
import androidx.media3.ui.AspectRatioFrameLayout.RESIZE_MODE_FIT
import androidx.media3.ui.AspectRatioFrameLayout.RESIZE_MODE_FIXED_HEIGHT
import androidx.media3.ui.AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH
import androidx.media3.ui.AspectRatioFrameLayout.RESIZE_MODE_ZOOM
import androidx.media3.ui.PlayerView
import video.api.player.R
import video.api.player.databinding.ExoPlayerLayoutBinding
import video.api.player.interfaces.IExoPlayerBasedPlayerView


/**
* The api.video player view class based on an ExoPlayer [PlayerView].
*
Expand All @@ -35,7 +38,7 @@ class ApiVideoExoPlayerView @JvmOverloads constructor(
* Shows or hides the full screen button
*/
var showFullScreenButton: Boolean = true
@OptIn(UnstableApi::class)
@SuppressLint("UnsafeOptInUsageError")
set(value) {
if (value) {
playerView.setFullscreenButtonClickListener {
Expand All @@ -60,7 +63,7 @@ class ApiVideoExoPlayerView @JvmOverloads constructor(
* Shows or hides the subtitles
*/
var showSubtitles: Boolean = true
@OptIn(UnstableApi::class)
@SuppressLint("UnsafeOptInUsageError")
set(value) {
if (value) {
playerView.subtitleView?.visibility = VISIBLE
Expand All @@ -72,6 +75,17 @@ class ApiVideoExoPlayerView @JvmOverloads constructor(
field = value
}

/**
* Sets or gets how the video is fitted in its parent view
*/
var viewFit: ViewFit
@SuppressLint("UnsafeOptInUsageError")
get() = ViewFit.fromValue(playerView.resizeMode)
@SuppressLint("UnsafeOptInUsageError")
set(value) {
playerView.resizeMode = value.value
}

init {
val a = context.obtainStyledAttributes(attrs, R.styleable.ApiVideoExoPlayerView)
try {
Expand All @@ -92,4 +106,39 @@ class ApiVideoExoPlayerView @JvmOverloads constructor(
*/
fun onFullScreenModeChanged(isFullScreen: Boolean) {}
}

/**
* The different ways to fit the video in its parent view
*/
@SuppressLint("UnsafeOptInUsageError")
enum class ViewFit(val value: Int) {
/**
* The video is resized to be contained in its parent view
*/
CONTAINS(RESIZE_MODE_FIT),

/**
* The video is resized to fit its parent view width. The height is adjusted to keep the aspect ratio.
*/
FIT_WIDTH(RESIZE_MODE_FIXED_WIDTH),

/**
* The video is resized to fit its parent view height. The width is adjusted to keep the aspect ratio.
*/
FIT_HEIGHT(RESIZE_MODE_FIXED_HEIGHT),

/**
* The video is resized to fill its parent view. Aspect ratio is not preserved.
*/
FILL(RESIZE_MODE_FILL),

/**
* The video is resized to fill its parent view. Aspect ratio is preserved.
*/
ZOOM(RESIZE_MODE_ZOOM);

companion object {
fun fromValue(value: Int) = entries.first { it.value == value }
}
}
}

0 comments on commit b7fa735

Please sign in to comment.