ryanheise / just_audio

Audio Player
1.03k stars 650 forks source link

fullPlaybackStateStream duplicated #129

Closed gmatiastor closed 4 years ago

gmatiastor commented 4 years ago

Hello,

I'm sure you can guide me to the right solution in this issue. Thanks in advance.

Which API doesn't behave as documented, and how does it misbehave? fullPlaybackStateStream.listen()

Possible error: fullPlaybackStateStream streams duplicated events . In particular, two similar events with "completed" stated are streamed , triggering twice the code I need to execute only once when playback is finished.

Minimal reproduction project

AudioPlayer p = AudioPlayer();
 p.setUrl("https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3").then((d) => p.play());
 p.fullPlaybackStateStream.listen((stateSnapshot) {
  // here is the code I need to trigger when stateSnapshot.state changes // ie: playing, completed..
});

Expected behavior Only one event is expected per state (connecting, stop, playing, completed)

Smartphone (please complete the following information): Android emulator

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 1.18.0-11.1.pre, on Microsoft Windows [Versión 10.0.18362.959], locale es-ES)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.0)
[√] VS Code (version 1.46.1)
[√] Connected device (3 available)

• No issues found!

Additional context Add any other context about the problem here.

ryanheise commented 4 years ago

That particular stream updates whenever the playback state changes, whenever the buffering state changes, and whenever the icy metadata changes. So if the icy metadata changes but the playback state stays the same, you will still get a new event emitted on that stream.

If you're only interested in the playback state, you can listen directly to AudioPlayer.playbackStateStream. If you're interested in both the playback state and the buffering state, there is currently no stream that combines those two, but you can select any combination of streams you want using rxdart:

newStream = Rx.combineLatest2(AudioPlayer.playbackStateStream, AudioPlayer.bufferingStream,
    (state, buffering) => BufferPlaybackState(state, buffering));

BufferPlaybackState doesn't exist, but you could invent your own class to encapsulate this set of data.

Originally, FullAudioPlaybackState just contained the playback state and buffering state, and I guess the icy metadata field was added in a PR. I can see it would still be useful to add a stream without the icy metadata, and this may come in a future release, but in the meantime you can still listen to the streams for the individual pieces of data you want and combine them with rxdart.

gmatiastor commented 4 years ago

Thank you very much!!

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with just_audio.