Skip to content

Commit

Permalink
Added media3 video player demo
Browse files Browse the repository at this point in the history
  • Loading branch information
myofficework000 committed Mar 20, 2024
1 parent 781b531 commit 378a478
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,49 @@ import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView

/**
* A composable function that creates a view for playing videos using ExoPlayer.
*/
@Composable
fun ExoPlayerView() {
// URL of the video to be played
val videoUrl = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"

// Get the current context
val context = LocalContext.current

// Initialize ExoPlayer
val exoPlayer = ExoPlayer.Builder(context).build()

// Create a media item from the video URL
val mediaSource = remember(videoUrl) {
MediaItem.fromUri(videoUrl)
}

// Set up ExoPlayer with the media source
LaunchedEffect(mediaSource) {
exoPlayer.setMediaItem(mediaSource)
exoPlayer.prepare()
}

// Release ExoPlayer when the composable is disposed
DisposableEffect(Unit) {
onDispose {
exoPlayer.release()
}
}

// Create an AndroidView for displaying the PlayerView
AndroidView(
// Create the PlayerView and set the ExoPlayer
factory = { ctx ->
PlayerView(ctx).apply {
player = exoPlayer
}
},
// Set the layout parameters for the PlayerView
modifier = Modifier
.fillMaxWidth()
.height(400.dp)
)
}
}

0 comments on commit 378a478

Please sign in to comment.