I'm trying to create a video player (exoPlayer) as...
# compose
c
I'm trying to create a video player (exoPlayer) as a composable and I'm following along with this article https://itnext.io/playing-a-video-with-jetpack-compose-10a453ff956 It gets me to the point where I'm playing a video which is nice, but I have 3 questions if anyone can chime in. 1. Is the
remember
method appropriate to make sure the composable doesn't reload the video on every compostion? 2. LaunchedEffect (I haven't used it before) seems like an interesting use case to use to update the fact that the video url has changed. Maybe the fact that it's called "Effect" is throwing me off, but would there be any other way to do this? 3. How does one properly dispose of this video playback. Does the composable clean up the androidView inside of it when I don't show it on screen anymore?
Sorry for the noob questions. My first time working with exoplayer AND with AndroidViews and so I want to make sure I understand everything so I don't run into a memory leak because of this video playing in the background of my app. (like spotify attached below)
s
1, 2. use
remember(key1 = "url") {...}
if getting url is not async. launchedEffect otherwise. 3. There is a
disposableEffect
API for clean up.
👍 1
h
First of all, I'm on mobile. Get ready for horrible formatting. 1. It's been a while since I worked closely with ExoPlayer. For that post and the code example, I just focused on Compose part. I'd say remembering one ExoPlayer instance for one VideoPlayer composable is enough. You should be the decider of what keys you'll chose for that
remember
call. 2. I thought of video URL as part of the state and ExoPlayer is surely not very compose friendly with its API. So,
LaunchedEffect
is a good candidate to update your player instance with the latest URL. 3. If the composable that holds your
AndroidView
leaves the composition, you can safely assume that
AndroidView
is cleaned up after. You are responsible for clearing ExoPlayer side of things.
DisposableEffect
should be enough.
c
Thanks @Halil Ozercan It looks like I should look up best practices for getting rid of/hiding/cleaning up exoPlayer and then tie those into DisposableEffect.
h
@Colton Idle that sounds perfect
💯 1