-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Race condition when playing/pausing audio #1687
Comments
The fact that this flag was negative (true when audio is off) lead to some confusion. Changing to `audioOn` standardizes the three audio-related settings options. This also addresses some lingering issues around music: namely bluefireteam/audioplayers#1687 and the inability of web apps to play sounds before user interaction.
The fact that this flag was negative (true when audio is off) lead to some confusion. Changing to `audioOn` standardizes the three audio-related settings options. This also addresses some lingering issues around music: namely bluefireteam/audioplayers#1687 and the inability of web apps to play sounds before user interaction.
Despite my arguments on Discord (no network, error, etc.) I think it's more consistent to set the state at the beginning before any async processing is happening (as you proposed). If an error occurs the the playing state should be reset. Tests for both of the cases should be included, if possible. |
Now that I think about: setting the source simply takes much longer than just doing the |
That works for me, thanks! FWIW, my current workaround is something like this: Future<void> _playCurrentSongInPlaylist() async {
_log.info(() => 'Playing ${_playlist.first} now.');
try {
await _musicPlayer.play(AssetSource('music/${_playlist.first.filename}'));
} catch (e) {
_log.severe('Could not play song ${_playlist.first}', e);
}
// Settings can change while the music player is preparing
// to play a song (i.e. during the `await` above).
if (!_settings!.audioOn.value || !_settings!.musicOn.value) {
try {
_log.fine('Settings changed while preparing to play song. '
'Pausing music.');
await _musicPlayer.pause();
} catch (e) {
_log.severe('Could not pause music player', e);
}
}
} |
I'm not able to run this at this time to confirm, but looking at the code and at the test — yes, this will solve my issue. |
Checklist
Current bug behaviour
When
AudioPlayer.pause()
is called a short time afterAudioPlayers.play()
is called, the pause will not take effect. The sound file will play in its entirety.Same goes for
AudioPlayer.stop()
instead of.pause()
.Expected behaviour
When
pause()
is the latest call to anAudioPlayer
, the audio doesn't play.Steps to reproduce
flutter run
on the code sampleCode sample
Code sample
Go to the official example's
lib/tabs/sources.dart
file and replace the_play()
method with the following:Affected platforms
Android, iOS, web, Windows, Linux, macOS
Platform details
No response
AudioPlayers Version
main, 5.2.0
Build mode
debug
Audio Files/URLs/Sources
No response
Screenshots
No response
Logs
No response
Related issues / more information
Note that everything works as expected if you do something like:
But in an app or game, events come from different sources. For example, settings can be updated (read from file) on startup, at almost the exact same moment as music is started. This can lead to the following:
play(...)
shared_preferences
, and they say "no audio"stop()
stop()
finishes before theplay()
can get to theresume()
stepFWIW, I couldn't replicate the behavior when using
resume()
(i.e. the source is already loaded). So I think the problem stems from the steps included in theplay()
method, which is not atomic.Related Discord discussion: https://discord.com/channels/509714518008528896/533299043686940692/1168866344742162463
Working on PR
no way
The text was updated successfully, but these errors were encountered: