I am creating a flutter live radio stream application with the just_audio
and just_audio_background
packages. But I have some issues with the background audio control and to reset the cache with the background controls so the user after a pause is listening the live audio not the buffered part.
I tried using the isLive
parameter with mixed success.
First of all, the audio seeker bar is disabled with this parameter which is good, and I need that, but when the user stops the playback and wants to restart it won't work.
If I don't use this parameter, then the notification control uses the pause method, which won't delete the buffer, and the seekbar is active and messes up the playback if you scroll with it.
My initialization code:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize JustAudioBackground only once
await JustAudioBackground.init(
androidNotificationChannelId: 'com.ryanheise.bg_demo.channel.audio',
androidNotificationChannelName: 'Audio playback',
androidNotificationOngoing: true,
);
This is in the HomeScreen widget:
class HomeScreen extends StatefulWidget {
final Map<String, dynamic> jsonData;
const HomeScreen({super.key, required this.jsonData});
@override
HomeScreenState createState() => HomeScreenState();
}
class HomeScreenState extends State<HomeScreen> {
final AudioPlayer _player = AudioPlayer();
late final MediaItem mediaItem;
Timer? _metadataTimer;
String? _currentTitle;
final StreamController<String?> _titleStreamController = StreamController<String?>.broadcast();
@override
void initState() {
super.initState();
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
statusBarColor: Colors.black,
));
mediaItem = MediaItem(
id: widget.jsonData['streamURL'],
album: "Koko",
title: widget.jsonData['nowPlayingPlaceHolder'] ?? "Radio",
artist: "Koko",
artUri: Uri.parse(widget.jsonData['radioLogoURL']),
isLive: true,
);
_initAudio();
if (Platform.isIOS) {
_startMetadataTimer();
}
}
['streamURL']
and the other parameters are fetched from an https link from a server.
I tried using the audio_service plugin, but I can't get it to work with this late mediaItem initialization way. So I can use the https json data.
Any help and tip would be appreciated.