'How to implement audio service with audioplayers flutter
Below code shows how i am using audioplayers without audio service
playFromFile(List textList, seek) async {
for (int i = seek; i < textList.length; i++) {
setState(() {
newSeek = i;
});
itemScrollController.scrollTo(
index: i,
duration: const Duration(seconds: 2),
curve: Curves.easeInOutCubic);
String text = textList[i].join(' ');
final bytes = await getAudio(text);
await audioPlugin.playBytes(bytes);
while (audioPlugin.state == PlayerState.PLAYING && !playFresh) {
await Future.delayed(const Duration(seconds: 1));
if (audioPlugin.state == PlayerState.PLAYING) {
audioPlugin.onPlayerCompletion.listen((onDone) async {
audioPlugin.state = PlayerState.COMPLETED;
await audioPlugin.release();
});
}
if (audioPlugin.state == PlayerState.COMPLETED) {
await audioPlugin.release();
break;
}
}
if (playFresh) break;
}
}
I want to implement this code using audio service so i can play audio in background . How to implement this with audio service so it will play in background. Please help as i am new to flutter and unable to solve this for days.
Solution 1:[1]
Solution will be quite simple. You implement this using different layer.
UI Layer - This will be your UI from where you going to click the play button.
Intermediate layer or Audio service Layer(Audio service class) - From your UI layer you going to called this audio service class play method.
Final layer (Actual audio player methods) - This will be final layer where your actual audio package mehtods resides. You called this layer from your intermediate layer.
Summary - UI layer play button -> Audio service Play method -> Audio player Play method
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wVV |
