-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from alsterverse/feature/robustify-player
Robustify player
- Loading branch information
Showing
13 changed files
with
215 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
plugins { | ||
//trick: for the same plugin versions in all sub-modules | ||
alias(libs.plugins.androidLibrary).apply(false) | ||
alias(libs.plugins.kotlinMultiplatform).apply(false) | ||
id("com.vanniktech.maven.publish") version "0.25.3" apply false | ||
alias(libs.plugins.androidLibrary) apply false | ||
alias(libs.plugins.kotlinMultiplatform) apply false | ||
alias(libs.plugins.compose.compiler) apply false | ||
alias(libs.plugins.vanniktechPublish) apply false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
player/src/androidMain/kotlin/se/alster/player/PlayerController.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package se.alster.player | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.LocalContext | ||
|
||
@Composable | ||
actual fun rememberPlayerController(): PlayerController { | ||
val context = LocalContext.current | ||
val playerController = remember { PlayerControllerAndroid(context) } | ||
return playerController | ||
} |
50 changes: 50 additions & 0 deletions
50
player/src/androidMain/kotlin/se/alster/player/ui/VideoView.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package se.alster.player.ui | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.annotation.OptIn | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clipToBounds | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.media3.common.util.UnstableApi | ||
import androidx.media3.ui.AspectRatioFrameLayout | ||
import androidx.media3.ui.PlayerView | ||
import se.alster.player.PlayerProvider | ||
import se.alster.player.R | ||
|
||
@OptIn(UnstableApi::class) | ||
@Composable | ||
internal actual fun InternalVideoView( | ||
modifier: Modifier, | ||
showControls: Boolean, | ||
aspectRatio: AspectRatio, | ||
playerProvider: PlayerProvider | ||
) { | ||
val player = playerProvider.player | ||
val currentView = LocalView.current as ViewGroup | ||
|
||
AndroidView( | ||
modifier = modifier.clipToBounds(), | ||
factory = { ctx -> | ||
(LayoutInflater.from(ctx).inflate( | ||
R.layout.video_texture_view, | ||
currentView, | ||
false | ||
) as PlayerView) | ||
.also { | ||
it.resizeMode = aspectRatio.toResizeMode() | ||
it.useController = showControls | ||
it.player = player | ||
} | ||
}, | ||
) | ||
} | ||
|
||
@OptIn(UnstableApi::class) | ||
private fun AspectRatio.toResizeMode(): Int = when (this) { | ||
AspectRatio.ScaleToFit -> AspectRatioFrameLayout.RESIZE_MODE_FIT | ||
AspectRatio.ScaleToFill -> AspectRatioFrameLayout.RESIZE_MODE_ZOOM | ||
AspectRatio.FillStretch -> AspectRatioFrameLayout.RESIZE_MODE_FILL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.media3.ui.PlayerView xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
app:keep_content_on_player_reset="true" | ||
app:resize_mode="zoom" | ||
app:surface_type="texture_view" | ||
app:use_controller="false" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
player/src/commonMain/kotlin/se/alster/player/PlayerProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
package se.alster.player | ||
|
||
/** | ||
* Provides a player instance. | ||
*/ | ||
expect interface PlayerProvider |
25 changes: 25 additions & 0 deletions
25
player/src/commonMain/kotlin/se/alster/player/ui/AspectRatio.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package se.alster.player.ui | ||
|
||
/** | ||
* The aspect ratio of the video. | ||
* | ||
* [ScaleToFit] - The video is scaled to fit inside the view. The aspect ratio is preserved. | ||
* [ScaleToFill] - The video is scaled to fill the view. The aspect ratio is preserved. | ||
* [FillStretch] - The video is stretched to fill the view. The aspect ratio is not preserved. | ||
*/ | ||
enum class AspectRatio { | ||
/** | ||
* The video is scaled to fit inside the view. The aspect ratio is preserved. | ||
*/ | ||
ScaleToFit, | ||
|
||
/** | ||
* The video is scaled to fill the view. The aspect ratio is preserved. | ||
*/ | ||
ScaleToFill, | ||
|
||
/** | ||
* The video is stretched to fill the view. The aspect ratio is not preserved. | ||
*/ | ||
FillStretch, | ||
} |
41 changes: 41 additions & 0 deletions
41
player/src/commonMain/kotlin/se/alster/player/ui/VideoView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package se.alster.player.ui | ||
|
||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import se.alster.player.PlayerProvider | ||
|
||
/** | ||
* A composable that displays a video player. The player is provided by the [playerProvider]. | ||
* Note: The [VideoView] does not handle the lifecycle of the player. The player should be | ||
* released when it is no longer needed. | ||
* | ||
* Example usage: | ||
* ```kotlin | ||
* val playerProvider = rememberPlayerProvider() | ||
* LaunchedEffect(playerProvider) { | ||
* playerProvider.loadVideo("https://example.com/video.mp4") | ||
* playerProvider.play() | ||
* } | ||
* VideoView( | ||
* modifier = Modifier.fillMaxSize(), | ||
* playerProvider = playerProvider | ||
* showControls = true | ||
* ) | ||
* ``` | ||
*/ | ||
@Composable | ||
fun VideoView( | ||
modifier: Modifier = Modifier.fillMaxSize(), | ||
showControls: Boolean = true, | ||
aspectRatio: AspectRatio = AspectRatio.ScaleToFill, | ||
playerProvider: PlayerProvider, | ||
) = InternalVideoView(modifier, showControls, aspectRatio, playerProvider) | ||
|
||
@Composable | ||
internal expect fun InternalVideoView( | ||
modifier: Modifier, | ||
showControls: Boolean, | ||
aspectRatio: AspectRatio, | ||
playerProvider: PlayerProvider | ||
) |
10 changes: 10 additions & 0 deletions
10
player/src/iosMain/kotlin/se/alster/player/PlayerController.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package se.alster.player | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
|
||
@Composable | ||
actual fun rememberPlayerController(): PlayerController { | ||
val playerController = remember { PlayerControllerIOS() } | ||
return playerController | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ import platform.AVFoundation.AVPlayer | |
|
||
actual interface PlayerProvider { | ||
val player: AVPlayer | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
player/src/iosMain/kotlin/se/alster/player/ui/VideoView.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package se.alster.player.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.viewinterop.UIKitView | ||
import platform.AVFoundation.AVLayerVideoGravity | ||
import platform.AVFoundation.AVLayerVideoGravityResize | ||
import platform.AVFoundation.AVLayerVideoGravityResizeAspect | ||
import platform.AVFoundation.AVLayerVideoGravityResizeAspectFill | ||
import platform.AVKit.AVPlayerViewController | ||
import se.alster.player.PlayerProvider | ||
|
||
@Composable | ||
internal actual fun InternalVideoView( | ||
modifier: Modifier, | ||
showControls: Boolean, | ||
aspectRatio: AspectRatio, | ||
playerProvider: PlayerProvider | ||
) { | ||
val player = playerProvider.player | ||
|
||
val avPlayerViewController = remember { AVPlayerViewController() } | ||
UIKitView( | ||
factory = { | ||
avPlayerViewController.player = player | ||
avPlayerViewController.videoGravity = aspectRatio.toVideoGravity() | ||
avPlayerViewController.showsPlaybackControls = showControls | ||
|
||
avPlayerViewController.view | ||
}, | ||
modifier = modifier, | ||
) | ||
} | ||
|
||
private fun AspectRatio.toVideoGravity(): AVLayerVideoGravity = when (this) { | ||
AspectRatio.ScaleToFit -> AVLayerVideoGravityResizeAspect | ||
AspectRatio.ScaleToFill -> AVLayerVideoGravityResizeAspectFill | ||
AspectRatio.FillStretch -> AVLayerVideoGravityResize | ||
} |