https://kotlinlang.org logo
#compose
Title
# compose
z

zt

06/27/2022, 2:16 AM
I'm trying to make UI for a media3 player but I'm not sure how to make the player variables like duration into states that cause recomposition
c

Chrimaeon

06/27/2022, 5:59 AM
This highly depends on how you handle your state in general. Is is i.e just
rememberState
in the composeabe or maybe a view model?
s

ste

06/27/2022, 10:07 AM
In my short experience,
duration
and
currentPosition
are best handled using a
Handler
in loop: https://github.com/vfsfitvnm/ViMusic/blob/master/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/PlayerState.kt#L84
z

zt

06/27/2022, 11:23 PM
I think that'll work for me. @ste you're the dev behind ViMusic? do you think you could help me implement a miniplayer similar to how you did it in ViMusic. I've been trying to make one in an app I'm developing
f

fengdai

06/28/2022, 3:17 AM
If you’re going to adopt ste’s solution. You’d better consider adding a
dispose
function for removing the callback from the
Handler
when
PlayState
is no longer used. Or
Handler
will cause leaking.
1
z

zt

06/28/2022, 3:21 AM
I'm gonna use the listener solution you showed
f

fengdai

06/28/2022, 3:29 AM
What about using my library directly? 😀
s

ste

06/28/2022, 8:55 AM
Feng is right. I just added a
dispose
function accordingly
👍🏻 1
z

zt

06/29/2022, 4:47 AM
Maybe theres an event to listen to for when the media changes, so a handler wouldn't be necessary?
s

ste

06/29/2022, 8:45 AM
No, if you want to obtain the
currentPosition
, you must poll it. You can either use a
Handler
or whatever you like - e.g. you could also poll it within the
rememeberPlayerState
using
produceState
20 Views