Open tomasbaran opened 3 years ago
probably related: https://github.com/bluefireteam/audioplayers/issues/118
I also posted this issue here: https://stackoverflow.com/questions/70225224/audio-not-looping-in-the-background?noredirect=1#comment124531255_70225224 to gather all information in one spot.
probably related: https://github.com/bluefireteam/audioplayers/issues/864
Try to change _player.setReleaseMode(ReleaseMode.LOOP);
into _player.setLoopMode(LoopMode.one);
@wahyu-handayani Thanks for your tip. Unfortunately, this doesn't help since setLoopMode is not an AudioPlayer() method:
@luanpotter Can you please comment on this issue since issues about this problem have been unresolved here for years. Shall I just move on a different package? For me it is crucial that the audio looping works in the background and I don't see any comment about it from its creators. Thanks.
Hi @tomasbaran right now I am trying to build an apps that can play a music on the background using audioplayers
package, and when I read this article https://denis-korovitskii.medium.com/flutter-demo-audioplayers-on-background-via-audio-service-c95d65c90ae1 , I am confused enough, so I read your code and trying to implement it. It is understandable when I read your code. If you don't mind, I would like to ask, how you initialized lightAnimation
? I try to call it by defining late LightAnimation lightAnimation;
first and then call await lightAnimation.audioHandler.playUrl(lightAnimation.audioUrl, volumeSetValue);
in an onPressed button, but it says that Unhandled Exception: LateInitializationError: Field 'lightAnimation' has not been initialized.
Let me see if I can be helpful. First off, I've never heard of late
term, so I'm not sure what it is supposed to do.
In my case, the app loads the library of LightAnimations that are on Firebase. So, I have in my app defined:
List<LightAnimation> localLibraryAnimations = [];
And once a user taps let's say on red light animations category then the app starts to load all the light animations from Firebase and assigning them to localLibraryAnimations
like this:
_currentAnimation = LightAnimation(
// animationCardImageBuilder: is for better loading experience but not essential in your case I guess
// animationCardImageBuilder: FutureBuilder(
// future: getImageFile(),
// builder: (context, snapshot) {
// if (snapshot.hasData)
// return Image.file(
// snapshot.data,
// fit: BoxFit.cover,
// width: double.infinity,
// height: double.infinity,
// );
// else if (snapshot.hasError) {
// return Text('Error#7: ${snapshot.error}', style: TextStyle(color: sColorPrimary56), textAlign: TextAlign.center);
// } else {
// return Center(
// child: Image.asset('assets/images/logo.png', width: 40),
// );
// }
// },
// ),
id: animation['animation_id'],
audioUrl: animation['audio_file_url'] ?? silentAudioFileUrl,
);
localLibraryAnimations.add(_currentAnimation);
So, to sum it up.
LightAnimation
one by oneaudioHandler
like this:
AudioPlayerHandler audioHandler = AudioPlayerHandler();
@tomasbaran Thank you very much for your detail explanation, I have read your comment and tried it on my own and finally.. got the good result. But there is something else that I would like to ask.. It was success to run on the background, but there is no something like notification on the background to pause or play or something that indicates we are playing a music. Is there a way to show it on the background ? By the way, I am so sorry for disturbing you
No problem @wahyu-handayani . I'm sure there is a way to show it but I don't know how since I didn't need this feature. Good luck!
Have you tried looping the audio in the background by any chance? If so, were you able to loop the audio in the background, please?
@tomasbaran I am able to do looping, here is the code and I call this inside onPressed method:
playerA = await audioCacheA.loop("Rain/${widget.musicAsset}.mp3");
where AudioPlayer playerA = AudioPlayer();
and AudioCache audioCacheA = AudioCache();
. It will loop in the background, but I don't know how to broadcast and show it in notification bar of the phone. I would like to make sure, so you don't use a feature to show to the user in a form of notification bar ?
Thanks @wahyu-handayani. Interesting, so looping in the background works for local assets (audioCache, your case) but not network assets like in my case.
No, I don't show or inform users in form of notification bar.
@tomasbaran you're welcome, glad to get your help
Unfortunately, I have bad news for your @wahyu-handayani. I have tried to loop in the background local asset just like you did via AudioCache()
but the looping stops in the background once you lock the screen. You can try it out yourself.
If you want the author to fix these issues, please upvote with thumb up these issues:
This should speed up the process of fixing the loop in the background issue. Thanks.
We would be happy to have an iOS developer, who can contribute to this library and submit a PR to this topic. Is it still valid for audioplayers: ^1.1.0
?
This issue is valid on iOS devices for audioplayers 3.0.1.
Problem still exists for audioplayers 5.2.1. Though from my test I have some extra info: In my app I have the option to run more than one players. This bug only happens though when only one of the players is working. As a result, my best guess is that ios kills the background before the player can loop. When a 2nd player is running though, it seems to keep the background alive so the loop goes through.
This opens at least the option for some pretty ugly workarounds.
Has anyone found it possible to achieve a seamless loop for network/url audio sources?
await _player.setReleaseMode(ReleaseMode.loop);
leaves a small gap at the start of each loop, even with seamlessly loop-able audio files.
Looping the audio is working in the foreground — but NOT in the background.
Full Description
I added this in my Info.plist
Code to Reproduce
audio_player_handler.dart:
I also have objects called:
LightAnimation
that has a property audioHandler defined like this:Then I just access from a different part of my codebase
Log Errors no errors
Screenshots
Platforms
flutter doctor -v
(this is optional).flutter doctor -v