Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:just_audio/just_audio.dart';
- import 'dart:async';
- import 'dart:io';
- class MusicManager {
- static final MusicManager _instance = MusicManager._internal();
- final AudioPlayer _audioPlayer = AudioPlayer();
- String? _currentTrack;
- String? _currentPolygonId;
- Duration? _currentTrackDuration;
- bool _isStopped = true;
- Function(String polygonId)? onTrackCompleted;
- Duration? get getCurrentTrackDuration => _currentTrackDuration;
- Duration get getCurrentPosition {
- if (_isStopped || _currentTrack == null) {
- return Duration.zero;
- }
- return _audioPlayer.position;
- }
- Duration? get getTotalDuration => _audioPlayer.duration;
- Stream<Duration> get positionStream {
- return _audioPlayer.positionStream.map((position) {
- if (_isStopped || _currentTrack == null) {
- return Duration.zero;
- }
- return position;
- });
- }
- Stream<Duration?> get durationStream => _audioPlayer.durationStream;
- String? _previousTrack;
- double _previousVolume = 0.5;
- Duration _previousPosition = Duration.zero;
- bool _overridePlaying = false;
- Timer? _overrideTimer;
- factory MusicManager() {
- return _instance;
- }
- MusicManager._internal() {
- _audioPlayer.playerStateStream.listen((state) {
- if (state.processingState == ProcessingState.idle) {
- _isStopped = true;
- }
- if (state.processingState == ProcessingState.completed &&
- !_overridePlaying &&
- _currentPolygonId != null) {
- debugPrint("🎵 [MusicManager] Piste terminée, notification...");
- onTrackCompleted?.call(_currentPolygonId!);
- }
- });
- }
- Future<void> play(String filepathMusic, {
- double volume = 0.5,
- Duration startAt = Duration.zero,
- String? polygonId
- }) async {
- if (filepathMusic.isEmpty) {
- debugPrint("Filepath is empty");
- return;
- }
- final file = File(filepathMusic);
- if (!await file.exists()) {
- debugPrint("❌ Fichier audio inexistant: $filepathMusic");
- return;
- }
- final fileSize = await file.length();
- debugPrint("📁 Taille du fichier audio: ${fileSize} bytes");
- if (fileSize == 0) {
- debugPrint("❌ Fichier audio vide: $filepathMusic");
- return;
- }
- if (filepathMusic == _currentTrack && !_isStopped) {
- debugPrint("Already playing this track");
- return;
- }
- if (_overridePlaying) {
- debugPrint("Override playing, cannot play another track");
- return;
- }
- _isStopped = true;
- _currentPolygonId = polygonId;
- await _audioPlayer.stop();
- try {
- debugPrint("🎵 Tentative de chargement du fichier: $filepathMusic");
- _currentTrackDuration = await _audioPlayer.setFilePath(filepathMusic);
- debugPrint("🎵 Durée de la piste: $_currentTrackDuration");
- if (_currentTrackDuration == null || _currentTrackDuration == Duration.zero) {
- debugPrint("❌ Impossible de charger le fichier audio ou durée nulle");
- return;
- }
- _audioPlayer.setVolume(volume);
- _currentTrack = filepathMusic;
- await _audioPlayer.seek(startAt);
- _isStopped = false;
- debugPrint("🎵 Démarrage de la lecture...");
- await _audioPlayer.play();
- debugPrint("🎵 Lecture démarrée avec succès");
- } catch (e) {
- debugPrint("❌ Erreur lors du chargement/lecture: $e");
- _isStopped = true;
- }
- debugPrint("🎵 [PLAY] Méthode play() terminée");
- }
- Future<void> playOverrideTrack(String overrideTrack, {double volume = 0.5, int duration = 15}) async {
- if (_currentTrack == null) {
- await play(overrideTrack, volume: volume);
- return;
- }
- _overrideTimer?.cancel();
- _previousTrack = _currentTrack;
- _previousVolume = _audioPlayer.volume;
- _previousPosition = _audioPlayer.position;
- await play(overrideTrack, volume: volume);
- _overridePlaying = true;
- _overrideTimer = Timer(Duration(seconds: duration), () async {
- _overridePlaying = false;
- await play(_previousTrack!, volume: _previousVolume, startAt: _previousPosition, polygonId: _currentPolygonId);
- });
- }
- void setVolume(double volume) {
- _audioPlayer.setVolume(volume);
- }
- Future<void> stop() async {
- debugPrint("🛑 Arrêt de la musique");
- _isStopped = true;
- _currentTrack = null;
- _currentPolygonId = null;
- await _audioPlayer.stop();
- await _audioPlayer.seek(Duration.zero);
- }
- Future<void> stopOverride() async {
- _isStopped = true;
- await _audioPlayer.stop();
- if (_overrideTimer != null && _overrideTimer!.isActive) {
- _overrideTimer?.cancel();
- _overridePlaying = false;
- if (_previousTrack != null) {
- await play(_previousTrack!, volume: _previousVolume, startAt: _previousPosition, polygonId: _currentPolygonId);
- }
- }
- }
- Future<void> pause() async {
- await _audioPlayer.pause();
- }
- Future<void> resume() async {
- if (_currentTrack != null && !_isStopped) {
- await _audioPlayer.play();
- }
- }
- bool isPlaying() {
- return _audioPlayer.playing && !_isStopped;
- }
- bool isStopped() {
- return _isStopped;
- }
- Future<void> resetPosition() async {
- _isStopped = true;
- await _audioPlayer.seek(Duration.zero);
- }
- void dispose() {
- _audioPlayer.dispose();
- _overrideTimer?.cancel();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement