My implementation loops in the foreground. It also works in the background. The issue is it does NOT loop in the background. The issue happens only on iOS physical device (not on Simulator, nor Android).
I'm using https://pub.dev/packages/audioplayers
plugin since https://pub.dev/packages/just_audio
was giving me all sorts of troubles especially with badly done audio caching which in the end always made every audio delay in the beginning of a track which was a no go for my case.
This is my audio_player_handler.dart
file:
import 'package:ambee2/models/animations/light_animation.dart';
import 'package:audio_service/audio_service.dart';
import 'package:flutter/material.dart';
// import 'package:just_audio/just_audio.dart';
import 'package:audioplayers/audioplayers.dart';
class AudioPlayerHandler extends BaseAudioHandler {
final _player = AudioPlayer();
AudioPlayerHandler() {
//set the audio to repeat itself once it's done
_player.setReleaseMode(ReleaseMode.LOOP);
}
Future<void> setUrl(String url) async => await _player.setUrl(url);
Future<void> playUrl(String url, double volumeValue) => _player.play(url, volume: volumeValue);
Future<void> resume() => _player.resume();
@override
Future<void> pause() => _player.pause();
@override
Future<void> stop() => _player.stop();
Future<void> setVolume(double newValue) => _player.setVolume(newValue);
}
I also have objects called: LightAnimation
that has a property audioHandler defined like this:
class LightAnimation {
AudioPlayerHandler audioHandler = AudioPlayerHandler();
LightAnimation({
this.audioUrl,
});
}
Then I just access from a different part of my codebase like this:
await lightAnimation.audioHandler.playUrl(lightAnimation.audioUrl, volumeSetValue);
Is my implementation wrong or is it just bug on a package side (https://pub.dev/packages/audioplayers)?