Live Streaming and Audio Equalizer with ExoPlayer in Jetpack Compose

Live Streaming and Audio Equalizer with ExoPlayer in Jetpack Compose

Setting Up ExoPlayer for Live Streaming

To combine ExoPlayer into our software for reside streaming, we have to comply with these key steps:

1. Add Dependencies

Include the mandatory dependencies within the venture’s construct.gradle file:

// Exoplayer dependencies
implementation("androidx.media3:media3-exoplayer:1.2.0")
implementation("androidx.media3:media3-ui:1.2.0")
implementation("androidx.media3:media3-exoplayer-hls:1.2.0")

These dependencies be sure that the appliance can leverage the options of ExoPlayer for reside streaming.

2. Create ExoPlayerSupervisor

We’ll create a supervisor class to deal with ExoPlayer situations. This ensures a single-player occasion all through the app’s lifecycle.

object ExoPlayerSupervisor {
personal var exoPlayer: ExoPlayer? = null

enjoyable getExoPlayer(context: Context): ExoPlayer {
if (exoPlayer == null) {
exoPlayer = ExoPlayer.Builder(context).construct()
}
return exoPlayer!!
}

enjoyable releaseExoPlayer() {
exoPlayer?.launch()
exoPlayer = null
}
}

3. Initialize ExoPlayer

Within your Composable perform, initialize ExoPlayer with a pattern HLS stream URL:

@Composable
enjoyable LiveStreamingDisplay screen() {

// Obtain the present context and lifecycle proprietor utilizing NativeContext and NativeLifecycleProprietor
val context = NativeContext.present
val lifecycleOwner = NativeLifecycleProprietor.present

// Remember the ExoPlayer occasion to persist throughout recompositions
val exoPlayer = bear in mind { ExoPlayerSupervisor.getExoPlayer(context) }

// Launch an impact to initialize ExoPlayer and arrange the media supply
LaunchedEffect(key1 = Unit) {

// Create a knowledge supply manufacturing unit for dealing with media requests
val dataSourceFactory = DefaultHttpDataSource.Factory()

// Define the URI for the pattern HLS stream
val uri = Uri.Builder()
.encodedPath("http://pattern.vodobox.web/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8")
.construct()
val mediaItem = MediaItem.Builder().setUri(uri).construct()

// Create an HlsMediaSource from the media merchandise for dealing with HTTP Live Streaming (HLS) content material
val internetVideoSource =
HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem)

exoPlayer.setMediaSource(internetVideoSource)
exoPlayer.put together()

// Will be utilized in later implementation for Equalizer
viewModel.onStart(exoPlayer.audioSessionId)
}

// ...
}

4. Display ExoPlayer View

Integrate the ExoPlayer view into your Composable perform

// ...
Box(modifier = Modifier.fillMaxSize()) {
AndroidView(
modifier =
Modifier.fillMaxWidth()
.aspectRatio(1.4f)
.padding(prime = 16.dp)
.background(Color.Black),
manufacturing unit = {
PlayerView(context).apply {
// Connect the ExoPlayer occasion to the PlayerView
participant = exoPlayer
// Configure ExoPlayer settings
exoPlayer.repeatMode = Player.REPEAT_MODE_ONE
exoPlayer.playWhenReady = false
useController = true
}
}
)
}
// ...

5. Observe Lifecycle Events and Release Resources

Set up DisposableEffects to watch lifecycle occasions and launch the ExoPlayer when the composable is disposed:

// ...
// Observe lifecycle occasions (e.g., app resume and pause)
// and modify ExoPlayer's playback state accordingly.
DisposableEffect(key1 = lifecycleOwner) {
val observer = LifecycleEventObserver { _, occasion ->
if (occasion == Lifecycle.Event.ON_RESUME) {
exoPlayer.playWhenReady = true
} else if (occasion == Lifecycle.Event.ON_PAUSE) {
exoPlayer.playWhenReady = false
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.take awayObserver(observer)
}
}

// Release the ExoPlayer when the composable is disposed
// This helps in correct useful resource administration
DisposableEffect(key1 = Unit) {
onDispose { ExoPlayerSupervisor.releaseExoPlayer() }
}
// ...

With the above code, we now have efficiently arrange ExoPlayer for reside streaming! You can take a look at the ultimate code from right here.

Player View

HI-FI News

by way of Android on Medium https://ift.tt/hXajOIq

December 20, 2023 at 08:45AM