cpd_2022_zi/lib/providers/audio_provider.dart

48 lines
1.1 KiB
Dart
Raw Normal View History

2023-03-04 12:14:29 +01:00
import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
class AudioProvider extends ChangeNotifier {
final AudioPlayer _audioPlayer = AudioPlayer();
bool _isMuted = false;
final Source _finishedSoundSource = AssetSource('finish.mp3');
final Source _beepSoundSource = AssetSource('beep.mp3');
StreamSubscription? _onCompleteSubscription;
bool get isMuted => _isMuted;
void stop() => _resetPlayer();
void playFinishSound() {
_resetPlayer();
_audioPlayer.play(_finishedSoundSource);
}
void mutePlayer() {
_isMuted = true;
_audioPlayer.setVolume(0);
notifyListeners();
}
void unMutePlayer() {
_isMuted = false;
_audioPlayer.setVolume(1);
notifyListeners();
}
//resets player position and delete listening subscription
void _resetPlayer() {
_audioPlayer.stop();
_onCompleteSubscription?.cancel();
}
void playSourceAfterBeep(AssetSource source) {
_resetPlayer();
_audioPlayer.play(_beepSoundSource);
_onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
_audioPlayer.play(source);
});
}
}