Skip to content

Commit

Permalink
[FEAT] Added PlayerView
Browse files Browse the repository at this point in the history
 - Added PlayerController and rememberPlayerController.
 [FIX] Fixed an issue with Toast causing the swipe in toast to confirm the action instead of dismissing it.
  • Loading branch information
iZakirSheikh committed Sep 17, 2024
1 parent f6bde4c commit acd7d3b
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 73 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId = "com.googol.android.apps.photos"
minSdk = 24
targetSdk = 35
versionCode = 22
versionName = "0.1.0-dev22"
versionCode = 23
versionName = "0.1.0-dev23"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
70 changes: 0 additions & 70 deletions foundation/src/main/java/com/zs/foundation/Player.kt

This file was deleted.

163 changes: 163 additions & 0 deletions foundation/src/main/java/com/zs/foundation/player/Player.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright 2024 Zakir Sheikh
*
* Created by 2024 on 17-09-2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.zs.foundation.player

import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.callbackFlow

/**
* A value class that wraps a [Player] instance and provides convenient access to its properties and methods.
*
* This class is designed to be used as a lightweight wrapper around the `Player` object, allowing you to interact with it
* in a more concise and expressive way.
*
* @property value The underlying [Player] instance.
* @see rememberPlayerController
*/
@JvmInline
value class PlayerController internal constructor(internal val value: Player){

companion object {
/**
* Represents an unset time value.
* @see C.TIME_UNSET
*/
val TIME_UNSET = C.TIME_UNSET

/**
* Player is idle.
* @see Player.STATE_IDLE
*/
val STATE_IDLE = Player.STATE_IDLE

/**
* Player is buffering.
* @see Player.STATE_BUFFERING
*/
val STATE_BUFFERING = Player.STATE_BUFFERING

/**
* Player is ready to play.
* @see Player.STATE_READY
*/
val STATE_READY = Player.STATE_READY

/**
* Playback has ended.
* @see Player.STATE_ENDED
*/
val STATE_ENDED = Player.STATE_ENDED
}

/**
* @see Player.getDuration
*/
val duration get() = value.duration

/**
* @see Player.getCurrentPosition
*/
val position get() = value.currentPosition

/**
* @see Player.getPlaybackState
*/
val state get() = value.playbackState

/**
* @see Player.getPlayWhenReady
*/
var playWhenReady get() = value.playWhenReady
set(value) { this.value.playWhenReady = value }

/**
* @see Player.isPlaying
*/
val isPlaying get() = value.isPlaying

/**
* Emits a Unit value whenever the [Player.getPlaybackState] changes.
*
* **Note:** This property must be stored in a variable to be active, as it creates a
* new [Flow] instance each time it's accessed.
*
* Usage:
* ```
* val mediaStateFlow = onMediaStateChanged
* ```
*
* @see state
*/
val onMediaStateChanged: Flow<Unit> get() = callbackFlow {
// Listener to capture playback state changes
val listener = object : Player.Listener {
override fun onPlaybackStateChanged(playbackState: Int) {
// Emit a unit value whenever the playback state changes
trySend(Unit)
}
}

// Add the listener to the player
value.addListener(listener)

// Remove the listener when the flow is closed
awaitClose { value.removeListener(listener) }
}


fun load(url: Uri){
value.setMediaItem(MediaItem.fromUri(url))
value.prepare()
}

fun play(playWhenReady: Boolean = true){
this.playWhenReady = playWhenReady
value.play()
}

fun pause() = value.pause()
fun stop() = value.stop()
}

/**
* Creates and remembers a [PlayerController] instance.
*/
@Composable
@NonRestartableComposable
fun rememberPlayerController(): PlayerController {
val context = LocalContext.current
return remember {
PlayerController(
value = ExoPlayer.Builder(context).build()
)
}
}
50 changes: 50 additions & 0 deletions foundation/src/main/java/com/zs/foundation/player/PlayerView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 Zakir Sheikh
*
* Created by 2024 on 17-09-2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.zs.foundation.player

import android.view.View
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.ui.PlayerView

@Composable
fun PlayerView(
controller: PlayerController,
modifier: Modifier = Modifier
) {
AndroidView(
modifier = modifier,
factory = {
PlayerView(it).apply {
player = controller.value
useController = false
clipToOutline = true
// Set the Background Color of the player as Solid Black Color.
setBackgroundColor(Color.Black.toArgb())
}
},
onRelease = {
it.player = null
it.visibility = View.GONE
}
)
}
2 changes: 1 addition & 1 deletion foundation/src/main/java/com/zs/foundation/toast/Toast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ internal fun Toast(
val dismissState = rememberDismissState(
confirmStateChange = {
val confirm = !isExpanded // Dismiss only if not expanded
if (confirm) value.action() // Execute action if confirmed
if (confirm) value.dismiss() // Execute action if confirmed
confirm
}
)
Expand Down

0 comments on commit acd7d3b

Please sign in to comment.