Skip to content

Commit

Permalink
Done FullscreenPlayer, added PiP to FullscreenPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Dec 16, 2024
1 parent 315d51c commit 45bba1d
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 96 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
android:name=".ui.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:supportsPictureInPicture="true"
android:enableOnBackInvokedCallback="true"
android:launchMode="singleTask">
<intent-filter>
Expand Down
71 changes: 59 additions & 12 deletions app/src/main/java/com/maxrave/simpmusic/extension/UIExt.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.maxrave.simpmusic.extension

import android.app.Activity
import android.app.PictureInPictureParams
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Point
import android.os.Build
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.annotation.ColorInt
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
Expand All @@ -18,10 +20,10 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyListItemInfo
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
Expand All @@ -48,8 +50,6 @@ import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.graphics.ColorUtils
import com.kmpalette.palette.graphics.Palette
import com.maxrave.simpmusic.ui.theme.md_theme_dark_background
Expand Down Expand Up @@ -395,10 +395,16 @@ fun LazyListState.animateScrollAndCentralizeItem(
}

@Composable
fun KeepScreenOn() = AndroidView(
factory = { View(it).apply { keepScreenOn = true } },
modifier = Modifier.size(0.1.dp)
)
fun KeepScreenOn() {
val context = LocalContext.current
DisposableEffect(Unit) {
val window = context.findActivity().window
window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
onDispose {
window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
}

@Composable
fun LazyListState.isScrollingUp(): Boolean {
Expand Down Expand Up @@ -470,10 +476,51 @@ fun Palette?.getColorFromPalette(): Color {
return Color(ColorUtils.setAlphaComponent(startColor, 255))
}

fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
fun Context.findActivity(): ComponentActivity {
var context = this
while (context is ContextWrapper) {
if (context is ComponentActivity) return context
context = context.baseContext
}
throw IllegalStateException("Picture in picture should be called in the context of an Activity")
}

@Composable
fun PipListenerPreAPI12() {
// [START android_compose_pip_pre12_listener]
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
val context = LocalContext.current
DisposableEffect(context) {
val onUserLeaveBehavior: () -> Unit = {
context.findActivity()
.enterPictureInPictureMode(PictureInPictureParams.Builder().build())
}
context.findActivity().addOnUserLeaveHintListener(
onUserLeaveBehavior
)
onDispose {
context.findActivity().removeOnUserLeaveHintListener(
onUserLeaveBehavior
)
}
}
} else {
Log.i("PiP info", "API does not support PiP")
}
// [END android_compose_pip_pre12_listener]
}

/**
* Android 12 and above Picture in Picture mode
*/
fun Modifier.pipModifier(context: Context) = this.onGloballyPositioned { layoutCoordinates ->
val builder = PictureInPictureParams.Builder()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.setAutoEnterEnabled(true)
}
Log.w("PiP info", "layoutCoordinates: $layoutCoordinates")
context.findActivity().setPictureInPictureParams(builder.build())
}

@RequiresOptIn(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.maxrave.simpmusic.ui.component

import android.os.Build
import android.util.Log
import android.view.TextureView
import androidx.compose.animation.Crossfade
Expand Down Expand Up @@ -45,7 +46,9 @@ import coil3.request.crossfade
import coil3.toCoilUri
import com.maxrave.simpmusic.common.Config
import com.maxrave.simpmusic.extension.KeepScreenOn
import com.maxrave.simpmusic.extension.PipListenerPreAPI12
import com.maxrave.simpmusic.extension.getScreenSizeInfo
import com.maxrave.simpmusic.extension.pipModifier
import org.koin.compose.koinInject
import org.koin.core.qualifier.named
import kotlin.math.roundToInt
Expand Down Expand Up @@ -166,7 +169,9 @@ fun MediaPlayerView(
fun MediaPlayerView(
player: ExoPlayer,
modifier: Modifier = Modifier,
pipSupport: Boolean = false,
) {
val context = LocalContext.current
var videoRatio by rememberSaveable {
mutableFloatStateOf(16f / 9)
}
Expand All @@ -179,6 +184,10 @@ fun MediaPlayerView(
mutableStateOf(false)
}

if (pipSupport) {
PipListenerPreAPI12()
}

val playerListener =
remember {
object : Player.Listener {
Expand Down Expand Up @@ -220,7 +229,13 @@ fun MediaPlayerView(
}

Box(
modifier,
modifier.then(
if (pipSupport && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Modifier.pipModifier(context)
} else {
Modifier
}
),
contentAlignment = Alignment.Center
) {
if (keepScreenOn) {
Expand Down
Loading

0 comments on commit 45bba1d

Please sign in to comment.