isLive flag for streams
Is your feature request related to a problem? Please describe.
I have spent a few days already trying to implement an isLive flag in my radio live stream player, but without success. I am playing one simple audio source, a radio stream.
Describe the solution you'd like
It would be really helpful if the audio player had a flag isLive which is true, when the player is currently playing live, and false if the player is a little behind in time (e.g. when the user paused and resumed the stream)
Describe alternatives you've considered I tried several approaches:
- inferring the live state from the
playerStateStream, e.g. settingisLive=trueif the player is in thereadystate and isplaying, but this is obviously not correct. if the player is paused and resumed, it already breaks - i tried to hold a flag myself for
isFirstStart, which is set to true initially. Once the user presses play, i doisLive=isFirstStartand setisFirstStart=falseat the same time. But this is also not working for all cases, as the sequence of the statesloading,bufferingandreadyis very random between Android and iOS. On Android, for example, it goes fromloadingtoready(playing=false) directly, and after pressing play, toready(playing=true, which is fine). Where on iOS, the sequence isloading,buffering,ready(withplaying=false), and then when pressing play:ready(playing=true, this is where i setisLive=isFirstStartand reset my flagisFirstStart=false),buffering(again?),ready(playing=true, here myisFirstStartis already false, so theisLiveflag is directly back atfalse) - inferring the
isLivefrom thepositionStream. i.e. always when the position value is e.g.< 1 secondandplaying==true, i setisLiveto true. which works fine on android. BUT, on iOS, thepositionStreamis just never getting any event, not even once. is this desired?
is there anything else I could try?
it would be really convenient if the audio player could offer an isLive flag directly
Could you do this by monitoring the position and duration?
Thanks for the response @ryanheise!
It is very complicated to infer the correct isLive state from these streams, as they behave totally different on Android and iOS:
positionStream:
- Android: continuously gives position updates, like it should be
- iOS: is triggered 1x before the first play(), with position 00:00:00. Then again 2x when pausing the player, with the current position
durationStream:
- Android: is triggered 1x before the first play(), with position
null, then never again - iOS: is triggered 2x before the first play(), with position 00:00:00, then never again
I have been trying for a while but I couldn't make it work. Any other ideas?
It sounds like an issue that positionStream and durationStream are not updating accurately on live streams, and fixing this might allow you to implement the logic you require. In that case, the way forward would be to fix this.
@ryanheise ok, i can create a separate bug ticket if you want.
another solution to determine isLive would be providing a stream that yields play, pause, stop events, triggered by the user. right now, i can of course track these events from the play/pause/stop button within my app, BUT when the user uses the notification play/pause button (from just_audio_background), this principle breaks again. That's why a stream for the user-triggered play/pause/stop events could help.