You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.3 KiB
Dart
45 lines
1.3 KiB
Dart
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
import 'package:just_audio/just_audio.dart';
|
|
import 'package:universal_html/html.dart' as html;
|
|
|
|
abstract class AudioService {
|
|
Future<void> playTone({required String path, bool isMute = false});
|
|
|
|
Future<void> listenAudioPlayerEvents({required Function() onToneCompleted});
|
|
|
|
Future<void> disposeAudioPlayer();
|
|
}
|
|
|
|
class AudioServiceImp implements AudioService {
|
|
AudioPlayer audioPlayerInstance;
|
|
|
|
AudioServiceImp({required this.audioPlayerInstance});
|
|
|
|
@override
|
|
Future<void> listenAudioPlayerEvents({required Function() onToneCompleted}) async {
|
|
audioPlayerInstance.playerStateStream.listen((playerState) async {
|
|
if (playerState.processingState == ProcessingState.completed) {
|
|
onToneCompleted();
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<void> playTone({required String path, bool isMute = false}) async {
|
|
// just_audio has web support - should work on both platforms
|
|
// Web note: First audio play may require user interaction due to browser autoplay policy
|
|
audioPlayerInstance.setAsset(path);
|
|
if (isMute) {
|
|
audioPlayerInstance.setVolume(0.0);
|
|
} else {
|
|
audioPlayerInstance.setVolume(1.0);
|
|
}
|
|
await audioPlayerInstance.play();
|
|
}
|
|
|
|
@override
|
|
Future<void> disposeAudioPlayer() async {
|
|
await audioPlayerInstance.dispose();
|
|
}
|
|
}
|