Advertisement
Guest User

Untitled

a guest
Jun 17th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:just_audio/just_audio.dart';
  3. import 'dart:async';
  4. import 'dart:io';
  5.  
  6. class MusicManager {
  7. static final MusicManager _instance = MusicManager._internal();
  8. final AudioPlayer _audioPlayer = AudioPlayer();
  9.  
  10. String? _currentTrack;
  11. String? _currentPolygonId;
  12. Duration? _currentTrackDuration;
  13. bool _isStopped = true;
  14.  
  15. Function(String polygonId)? onTrackCompleted;
  16.  
  17. Duration? get getCurrentTrackDuration => _currentTrackDuration;
  18.  
  19. Duration get getCurrentPosition {
  20. if (_isStopped || _currentTrack == null) {
  21. return Duration.zero;
  22. }
  23. return _audioPlayer.position;
  24. }
  25.  
  26. Duration? get getTotalDuration => _audioPlayer.duration;
  27.  
  28. Stream<Duration> get positionStream {
  29. return _audioPlayer.positionStream.map((position) {
  30. if (_isStopped || _currentTrack == null) {
  31. return Duration.zero;
  32. }
  33. return position;
  34. });
  35. }
  36.  
  37. Stream<Duration?> get durationStream => _audioPlayer.durationStream;
  38.  
  39. String? _previousTrack;
  40. double _previousVolume = 0.5;
  41. Duration _previousPosition = Duration.zero;
  42. bool _overridePlaying = false;
  43. Timer? _overrideTimer;
  44.  
  45. factory MusicManager() {
  46. return _instance;
  47. }
  48.  
  49. MusicManager._internal() {
  50. _audioPlayer.playerStateStream.listen((state) {
  51. if (state.processingState == ProcessingState.idle) {
  52. _isStopped = true;
  53. }
  54.  
  55. if (state.processingState == ProcessingState.completed &&
  56. !_overridePlaying &&
  57. _currentPolygonId != null) {
  58. debugPrint("🎵 [MusicManager] Piste terminée, notification...");
  59. onTrackCompleted?.call(_currentPolygonId!);
  60. }
  61. });
  62. }
  63.  
  64. Future<void> play(String filepathMusic, {
  65. double volume = 0.5,
  66. Duration startAt = Duration.zero,
  67. String? polygonId
  68. }) async {
  69. if (filepathMusic.isEmpty) {
  70. debugPrint("Filepath is empty");
  71. return;
  72. }
  73.  
  74. final file = File(filepathMusic);
  75. if (!await file.exists()) {
  76. debugPrint("❌ Fichier audio inexistant: $filepathMusic");
  77. return;
  78. }
  79.  
  80. final fileSize = await file.length();
  81. debugPrint("📁 Taille du fichier audio: ${fileSize} bytes");
  82.  
  83. if (fileSize == 0) {
  84. debugPrint("❌ Fichier audio vide: $filepathMusic");
  85. return;
  86. }
  87.  
  88. if (filepathMusic == _currentTrack && !_isStopped) {
  89. debugPrint("Already playing this track");
  90. return;
  91. }
  92. if (_overridePlaying) {
  93. debugPrint("Override playing, cannot play another track");
  94. return;
  95. }
  96.  
  97. _isStopped = true;
  98. _currentPolygonId = polygonId;
  99.  
  100. await _audioPlayer.stop();
  101.  
  102. try {
  103. debugPrint("🎵 Tentative de chargement du fichier: $filepathMusic");
  104. _currentTrackDuration = await _audioPlayer.setFilePath(filepathMusic);
  105. debugPrint("🎵 Durée de la piste: $_currentTrackDuration");
  106.  
  107. if (_currentTrackDuration == null || _currentTrackDuration == Duration.zero) {
  108. debugPrint("❌ Impossible de charger le fichier audio ou durée nulle");
  109. return;
  110. }
  111.  
  112. _audioPlayer.setVolume(volume);
  113. _currentTrack = filepathMusic;
  114. await _audioPlayer.seek(startAt);
  115.  
  116. _isStopped = false;
  117. debugPrint("🎵 Démarrage de la lecture...");
  118. await _audioPlayer.play();
  119. debugPrint("🎵 Lecture démarrée avec succès");
  120.  
  121. } catch (e) {
  122. debugPrint("❌ Erreur lors du chargement/lecture: $e");
  123. _isStopped = true;
  124. }
  125. debugPrint("🎵 [PLAY] Méthode play() terminée");
  126. }
  127.  
  128. Future<void> playOverrideTrack(String overrideTrack, {double volume = 0.5, int duration = 15}) async {
  129. if (_currentTrack == null) {
  130. await play(overrideTrack, volume: volume);
  131. return;
  132. }
  133.  
  134. _overrideTimer?.cancel();
  135. _previousTrack = _currentTrack;
  136. _previousVolume = _audioPlayer.volume;
  137. _previousPosition = _audioPlayer.position;
  138.  
  139. await play(overrideTrack, volume: volume);
  140. _overridePlaying = true;
  141.  
  142. _overrideTimer = Timer(Duration(seconds: duration), () async {
  143. _overridePlaying = false;
  144. await play(_previousTrack!, volume: _previousVolume, startAt: _previousPosition, polygonId: _currentPolygonId);
  145. });
  146. }
  147.  
  148. void setVolume(double volume) {
  149. _audioPlayer.setVolume(volume);
  150. }
  151.  
  152. Future<void> stop() async {
  153. debugPrint("🛑 Arrêt de la musique");
  154. _isStopped = true;
  155. _currentTrack = null;
  156. _currentPolygonId = null;
  157. await _audioPlayer.stop();
  158. await _audioPlayer.seek(Duration.zero);
  159. }
  160.  
  161. Future<void> stopOverride() async {
  162. _isStopped = true;
  163. await _audioPlayer.stop();
  164.  
  165. if (_overrideTimer != null && _overrideTimer!.isActive) {
  166. _overrideTimer?.cancel();
  167. _overridePlaying = false;
  168.  
  169. if (_previousTrack != null) {
  170. await play(_previousTrack!, volume: _previousVolume, startAt: _previousPosition, polygonId: _currentPolygonId);
  171. }
  172. }
  173. }
  174.  
  175. Future<void> pause() async {
  176. await _audioPlayer.pause();
  177. }
  178.  
  179. Future<void> resume() async {
  180. if (_currentTrack != null && !_isStopped) {
  181. await _audioPlayer.play();
  182. }
  183. }
  184.  
  185. bool isPlaying() {
  186. return _audioPlayer.playing && !_isStopped;
  187. }
  188.  
  189. bool isStopped() {
  190. return _isStopped;
  191. }
  192.  
  193. Future<void> resetPosition() async {
  194. _isStopped = true;
  195. await _audioPlayer.seek(Duration.zero);
  196. }
  197.  
  198. void dispose() {
  199. _audioPlayer.dispose();
  200. _overrideTimer?.cancel();
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement