@@ -12,33 +12,57 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
1212 // Helper to access custom methods of our handler
1313 MusicPlayerHandler get _handler => _audioHandler as MusicPlayerHandler ;
1414
15+ // @override
16+ // Future<void> setQueue(List<SongEntity> songs, int initialIndex) async {
17+ // final mediaItems = songs.map((song) {
18+ // final uniqueId =
19+ // DateTime.now().microsecondsSinceEpoch.toString() +
20+ // song.id.toString(); // Simple unique ID
21+ // return MediaItem(
22+ // id: song.id.toString(),
23+ // album: song.album,
24+ // title: song.title,
25+ // artist: song.artist,
26+ // artUri: song.albumId != null
27+ // ? Uri.parse(
28+ // "content://media/external/audio/albumart/${song.albumId}",
29+ // )
30+ // : null,
31+ // duration: Duration(milliseconds: song.duration.toInt()),
32+ // extras: {'url': song.path, 'uniqueId': uniqueId},
33+ // );
34+ // }).toList();
35+
36+ // await _handler.setQueueItems(items: mediaItems, initialIndex: initialIndex);
37+ // }
38+
39+ /// Here is the new suggested method by claude to make sure that each song has its unique id since the old commented one was doing a great job but the problem was that the map() function is sync so in AOT it is so fast that it can give multiple songgs the same "unique id" which defeats the while purpose of a unuique id , so this implementation below solves that problem.///
1540 @override
1641 Future <void > setQueue (List <SongEntity > songs, int initialIndex) async {
17- final mediaItems = songs.map ((song ) {
18- final uniqueId = DateTime . now ().microsecondsSinceEpoch. toString () +
19- song.id. toString (); // Simple unique ID
42+ final mediaItems = songs.asMap ().entries. map ((entry ) {
43+ final index = entry.key;
44+ final song = entry.value;
2045 return MediaItem (
2146 id: song.id.toString (),
22- album: song.album,
2347 title: song.title,
2448 artist: song.artist,
49+ album: song.album,
2550 artUri: song.albumId != null
2651 ? Uri .parse (
2752 "content://media/external/audio/albumart/${song .albumId }" ,
2853 )
2954 : null ,
3055 duration: Duration (milliseconds: song.duration.toInt ()),
31- extras: {'url' : song.path, 'uniqueId' : uniqueId },
56+ extras: {'url' : song.path, 'uniqueId' : '${ song . id }_$ index ' },
3257 );
3358 }).toList ();
34-
3559 await _handler.setQueueItems (items: mediaItems, initialIndex: initialIndex);
3660 }
3761
3862 @override
3963 Future <void > addQueueItem (SongEntity song) async {
40- final uniqueId = DateTime . now ().microsecondsSinceEpoch. toString () +
41- song.id.toString ();
64+ final uniqueId =
65+ DateTime . now ().microsecondsSinceEpoch. toString () + song.id.toString ();
4266 final item = MediaItem (
4367 id: song.id.toString (),
4468 album: song.album,
@@ -140,17 +164,17 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
140164
141165 @override
142166 Stream <int > get loopModeStream => _audioHandler.playbackState.map ((state) {
143- switch (state.repeatMode) {
144- case AudioServiceRepeatMode .none:
145- return 0 ;
146- case AudioServiceRepeatMode .all:
147- return 1 ;
148- case AudioServiceRepeatMode .one:
149- return 2 ;
150- default :
151- return 0 ;
152- }
153- }).distinct ();
167+ switch (state.repeatMode) {
168+ case AudioServiceRepeatMode .none:
169+ return 0 ;
170+ case AudioServiceRepeatMode .all:
171+ return 1 ;
172+ case AudioServiceRepeatMode .one:
173+ return 2 ;
174+ default :
175+ return 0 ;
176+ }
177+ }).distinct ();
154178
155179 @override
156180 Stream <Duration > get positionStream => AudioService .position;
@@ -160,10 +184,9 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
160184 _audioHandler.mediaItem.map ((item) => item? .duration ?? Duration .zero);
161185
162186 @override
163- Stream <void > get playerCompleteStream => _audioHandler.playbackState
164- .where ((state) => state.processingState == AudioProcessingState .completed)
165- .map ((event) => null )
166- .distinct ();
187+ Stream <void > get playerCompleteStream => _audioHandler.playbackState.where (
188+ (state) => state.processingState == AudioProcessingState .completed,
189+ );
167190
168191 @override
169192 Stream <SongEntity ?> get currentSongStream =>
@@ -205,8 +228,8 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
205228
206229 @override
207230 Future <void > playNext (SongEntity song) async {
208- final uniqueId = DateTime . now ().microsecondsSinceEpoch. toString () +
209- song.id.toString ();
231+ final uniqueId =
232+ DateTime . now ().microsecondsSinceEpoch. toString () + song.id.toString ();
210233 final item = MediaItem (
211234 id: song.id.toString (),
212235 album: song.album,
0 commit comments