Skip to content

Commit

Permalink
queue option inits
Browse files Browse the repository at this point in the history
  • Loading branch information
HemantKArya committed Mar 27, 2024
1 parent db13ad5 commit 3a83bf8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 19 deletions.
1 change: 0 additions & 1 deletion lib/blocs/mediaPlayer/bloomee_player_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class BloomeePlayerCubit extends Cubit<BloomeePlayerState> {
Future<void> close() {
EasyDebounce.cancelAll();
bloomeePlayer.stop();
bloomeePlayer.currentQueueName.close();
bloomeePlayer.audioPlayer.dispose();
return super.close();
}
Expand Down
13 changes: 13 additions & 0 deletions lib/model/songModel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ class MediaItemModel extends MediaItem {
}
}

MediaItemModel mediaItem2MediaItemModel(MediaItem mediaItem) {
return MediaItemModel(
id: mediaItem.id,
title: mediaItem.title,
album: mediaItem.album,
artUri: mediaItem.artUri,
artist: mediaItem.artist,
extras: mediaItem.extras,
genre: mediaItem.genre,
duration: mediaItem.duration,
);
}

MediaItemDB MediaItem2MediaItemDB(MediaItem mediaItem) {
return MediaItemDB(
title: mediaItem.title,
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/screen/audioPlayer_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class _AudioPlayerViewState extends State<AudioPlayerView> {
stream: context
.watch<BloomeePlayerCubit>()
.bloomeePlayer
.currentQueueName,
.queueTitle,
builder: (context, snapshot) {
return InkWell(
onTap: () {
Expand Down
8 changes: 2 additions & 6 deletions lib/screens/screen/explore_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ class _ExploreScreenState extends State<ExploreScreen> {
context
.read<BloomeePlayerCubit>()
.bloomeePlayer
.loadPlaylist(
MediaPlaylist(
mediaItems: [e],
albumName: "Recently",
),
doPlay: true,
.addQueueItem(
e,
);
},
onOptionsTap: () =>
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/screen/library_views/playlist_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class PlaylistView extends StatelessWidget {
stream: context
.watch<BloomeePlayerCubit>()
.bloomeePlayer
.currentQueueName,
.queueTitle,
builder: (context, snapshot) {
if (snapshot.hasData &&
snapshot.data == playListName) {
Expand Down
9 changes: 9 additions & 0 deletions lib/screens/widgets/more_bottom_sheet.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:Bloomee/blocs/add_to_playlist/cubit/add_to_playlist_cubit.dart';
import 'package:Bloomee/blocs/mediaPlayer/bloomee_player_cubit.dart';
import 'package:Bloomee/model/songModel.dart';
import 'package:Bloomee/routes_and_consts/global_str_consts.dart';
import 'package:Bloomee/screens/widgets/snackbar.dart';
Expand Down Expand Up @@ -76,6 +77,10 @@ void showMoreBottomSheet(
),
onTap: () {
Navigator.pop(context);
context
.read<BloomeePlayerCubit>()
.bloomeePlayer
.addQueueItem(song, doPlay: false);
},
),
ListTile(
Expand All @@ -94,6 +99,10 @@ void showMoreBottomSheet(
),
onTap: () {
Navigator.pop(context);
context
.read<BloomeePlayerCubit>()
.bloomeePlayer
.addQueueItem(song, atLast: true, doPlay: false);
},
),
ListTile(
Expand Down
75 changes: 65 additions & 10 deletions lib/services/bloomeePlayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import 'package:just_audio/just_audio.dart';
import 'package:rxdart/rxdart.dart';
import 'package:Bloomee/model/songModel.dart';
import 'package:Bloomee/repository/Youtube/youtube_api.dart';
import 'package:rxdart/subjects.dart';

import '../model/MediaPlaylistModel.dart';

class BloomeeMusicPlayer extends BaseAudioHandler
with SeekHandler, QueueHandler {
late AudioPlayer audioPlayer;

List<MediaItemModel> currentPlaylist = [];
BehaviorSubject<String> currentQueueName =
BehaviorSubject<String>.seeded("Empty");
int currentQueueIdx = 0;
int currentPlaylistIdx = 0;
BehaviorSubject<bool> fromPlaylist = BehaviorSubject<bool>.seeded(false);

BehaviorSubject<bool> isLinkProcessing = BehaviorSubject<bool>.seeded(false);
int currentPlayingIdx = 0;
Expand Down Expand Up @@ -65,7 +68,8 @@ class BloomeeMusicPlayer extends BaseAudioHandler
));
}

MediaItemModel get currentMedia => currentPlaylist[currentPlayingIdx];
MediaItemModel get currentMedia =>
mediaItem2MediaItemModel(queue.value[currentPlayingIdx]);

@override
Future<void> play() async {
Expand All @@ -86,8 +90,9 @@ class BloomeeMusicPlayer extends BaseAudioHandler

Future<void> loadPlaylist(MediaPlaylist mediaList,
{int idx = 0, bool doPlay = false}) async {
currentPlaylist = mediaList.mediaItems;
currentQueueName.add(mediaList.albumName);
fromPlaylist.add(true);
queue.add(mediaList.mediaItems);
queueTitle.add(mediaList.albumName);
await prepare4play(idx: idx, doPlay: doPlay);
// if (doPlay) play();
}
Expand Down Expand Up @@ -136,7 +141,7 @@ class BloomeeMusicPlayer extends BaseAudioHandler
}

Future<void> prepare4play({int idx = 0, bool doPlay = false}) async {
if (currentPlaylist.isNotEmpty) {
if (queue.value.isNotEmpty) {
currentPlayingIdx = idx;
await playMediaItem(currentMedia, doPlay: doPlay);
BloomeeDBService.putRecentlyPlayed(MediaItem2MediaItemDB(currentMedia));
Expand All @@ -147,12 +152,14 @@ class BloomeeMusicPlayer extends BaseAudioHandler
Future<void> rewind() async {
if (audioPlayer.processingState == ProcessingState.ready) {
await audioPlayer.seek(Duration.zero);
} else if (audioPlayer.processingState == ProcessingState.completed) {
await prepare4play(idx: currentPlayingIdx);
}
}

@override
Future<void> skipToNext() async {
if (currentPlayingIdx < (currentPlaylist.length - 1)) {
if (currentPlayingIdx < (queue.value.length - 1)) {
currentPlayingIdx++;
prepare4play(idx: currentPlayingIdx);
// log("skippingNext-------", name: "bloomeePlayer");
Expand Down Expand Up @@ -191,12 +198,60 @@ class BloomeeMusicPlayer extends BaseAudioHandler
}

@override
Future<void> addQueueItem(MediaItem mediaItem) async {
queue.add([mediaItem]);
Future<void> insertQueueItem(int index, MediaItem mediaItem) async {
if (index < queue.value.length) {
queue.value.insert(index, mediaItem);
} else {
queue.add(queue.value..add(mediaItem));
}
}

@override
Future<void> addQueueItem(MediaItem mediaItem,
{bool doPlay = true, bool atLast = false}) async {
if (fromPlaylist.value) {
fromPlaylist.add(false);
if (!doPlay) {
queue.add([currentMedia, mediaItem]);
if (audioPlayer.processingState == ProcessingState.completed) {
queue.add([mediaItem]);
await prepare4play(idx: 0, doPlay: doPlay);
}
} else {
queue.add([mediaItem]);
await prepare4play(idx: 0, doPlay: doPlay);
}
queueTitle.add("Queue");
} else {
if (atLast) {
queue.add(queue.value..add(mediaItem));
} else if (currentPlayingIdx >= queue.value.length - 1 ||
queue.value.isEmpty) {
queue.add(queue.value..add(mediaItem));
if (doPlay) {
await prepare4play(idx: queue.value.length - 1, doPlay: doPlay);
} else if (audioPlayer.processingState == ProcessingState.completed ||
queue.value.length == 1) {
await prepare4play(idx: queue.value.length - 1, doPlay: doPlay);
}
} else {
queue.add(queue.value..insert(currentPlayingIdx + 1, mediaItem));
if (doPlay) {
await prepare4play(idx: currentPlayingIdx + 1, doPlay: true);
}
}
}
}

@override
Future<void> addQueueItems(List<MediaItem> mediaItems) async {
Future<void> addQueueItems(List<MediaItem> mediaItems,
{String queueName = "Queue"}) async {
queue.add(mediaItems);
queueTitle.add(queueName);
}

@override
Future<void> removeQueueItemAt(int index) async {
queue.value.removeAt(index);
}
}

0 comments on commit 3a83bf8

Please sign in to comment.