48 lines
1.1 KiB
Dart
48 lines
1.1 KiB
Dart
|
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);
|
||
|
});
|
||
|
}
|
||
|
}
|