Skip to content

Commit

Permalink
fix: macos build error, mobile player duration and playing state and …
Browse files Browse the repository at this point in the history
…background disposal of player
  • Loading branch information
Kingkor Roy Tirtho committed May 1, 2023
1 parent 6430a25 commit be91e33
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/components/album/album_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class AlbumCard extends HookConsumerWidget {
@override
Widget build(BuildContext context, ref) {
final playlist = ref.watch(PlaylistQueueNotifier.provider);
final playing = useStream(audioPlayer.playingStream).data ?? false;
final playing =
useStream(audioPlayer.playingStream).data ?? audioPlayer.isPlaying;
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);
final queryClient = useQueryClient();
final query = queryClient
Expand Down
4 changes: 3 additions & 1 deletion lib/components/player/player_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class PlayerControls extends HookConsumerWidget {
[]);
final playlist = ref.watch(PlaylistQueueNotifier.provider);
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);
final playing = useStream(audioPlayer.playingStream).data ?? false;

final playing =
useStream(audioPlayer.playingStream).data ?? audioPlayer.isPlaying;
final buffering = useStream(audioPlayer.bufferingStream).data ?? true;
final theme = Theme.of(context);

Expand Down
3 changes: 2 additions & 1 deletion lib/components/player/player_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class PlayerOverlay extends HookConsumerWidget {
);
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);
final playlist = ref.watch(PlaylistQueueNotifier.provider);
final playing = useStream(audioPlayer.playingStream).data ?? false;
final playing =
useStream(audioPlayer.playingStream).data ?? audioPlayer.isPlaying;

final theme = Theme.of(context);
final textColor = theme.colorScheme.primary;
Expand Down
3 changes: 2 additions & 1 deletion lib/components/playlist/playlist_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class PlaylistCard extends HookConsumerWidget {
Widget build(BuildContext context, ref) {
final playlistQueue = ref.watch(PlaylistQueueNotifier.provider);
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);
final playing = useStream(audioPlayer.playingStream).data ?? false;
final playing =
useStream(audioPlayer.playingStream).data ?? audioPlayer.isPlaying;
final queryBowl = QueryClient.of(context);
final query = queryBowl.getQuery<List<Track>, dynamic>(
"playlist-tracks/${playlist.id}",
Expand Down
11 changes: 9 additions & 2 deletions lib/hooks/use_progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ Tuple4<double, Duration, Duration, double> useProgress(WidgetRef ref) {
useStream(audioPlayer.bufferedPositionStream).data?.inSeconds ?? 0;
final playlistNotifier = ref.watch(PlaylistQueueNotifier.notifier);

final duration = useStream(audioPlayer.durationStream).data ?? Duration.zero;
// Duration future is needed for getting the duration of the song
// as stream can be null when no event occurs (Mostly needed for android)
final durationFuture = useFuture(audioPlayer.duration);
final duration = useStream(audioPlayer.durationStream).data ??
durationFuture.data ??
Duration.zero;
final positionFuture = useFuture(audioPlayer.position);
final positionSnapshot = useStream(audioPlayer.positionStream);

final position = positionSnapshot.data ?? Duration.zero;
final position =
positionSnapshot.data ?? positionFuture.data ?? Duration.zero;

final sliderMax = duration.inSeconds;
final sliderValue = position.inSeconds;
Expand Down
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@ class SpotubeState extends ConsumerState<Spotube> {

useInitSysTray(ref);

/// For enabling hot reload for audio player
useEffect(() {
return () {
/// For enabling hot reload for audio player
if (!kDebugMode) return;
audioPlayer.dispose();
youtube.close();
};
Expand Down
15 changes: 8 additions & 7 deletions lib/services/audio_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ class SpotubeAudioPlayer {
// stream getters
Stream<Duration> get durationStream {
if (apSupportedPlatform) {
return _audioPlayer!.onDurationChanged;
return _audioPlayer!.onDurationChanged.asBroadcastStream();
} else {
throw UnimplementedError();
}
}

Stream<Duration> get positionStream {
if (apSupportedPlatform) {
return _audioPlayer!.onPositionChanged;
return _audioPlayer!.onPositionChanged.asBroadcastStream();
} else {
throw UnimplementedError();
}
Expand All @@ -69,15 +69,15 @@ class SpotubeAudioPlayer {
Stream<Duration> get bufferedPositionStream {
if (apSupportedPlatform) {
// audioplayers doesn't have the capability to get buffered position
return const Stream.empty();
return const Stream<Duration>.empty().asBroadcastStream();
} else {
throw UnimplementedError();
}
}

Stream<void> get completedStream {
if (apSupportedPlatform) {
return _audioPlayer!.onPlayerComplete;
return _audioPlayer!.onPlayerComplete.asBroadcastStream();
} else {
throw UnimplementedError();
}
Expand All @@ -87,23 +87,24 @@ class SpotubeAudioPlayer {
if (apSupportedPlatform) {
return _audioPlayer!.onPlayerStateChanged.map((state) {
return state == ap.PlayerState.playing;
});
}).asBroadcastStream();
} else {
throw UnimplementedError();
}
}

Stream<bool> get bufferingStream {
if (apSupportedPlatform) {
return Stream.value(false);
return Stream.value(false).asBroadcastStream();
} else {
throw UnimplementedError();
}
}

Stream<PlayerState> get playerStateStream =>
_audioPlayer!.onPlayerStateChanged
.map((state) => PlayerState.fromApPlayerState(state));
.map((state) => PlayerState.fromApPlayerState(state))
.asBroadcastStream();

// regular info getter

Expand Down
4 changes: 2 additions & 2 deletions macos/Runner/DebugProfile.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<key>com.apple.security.network.client</key>
<true />
<!-- Just Audio Config -->
<key>NSAppTransportSecurity</key>
<!-- <key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true />
</dict>
</dict> -->
<!-- Requires Certification -->
<!-- <key>keychain-access-groups</key>
<array /> -->
Expand Down
6 changes: 3 additions & 3 deletions macos/Runner/Release.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<true />
<key>com.apple.security.network.server</key>
<true />
<!-- Just Audio Config -->
<key>com.apple.security.network.client</key>
<true />
<key>NSAppTransportSecurity</key>
<!-- Just Audio Config -->
<!-- <key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true />
</dict>
</dict> -->
<!-- Requires Certification -->
<!-- <key>keychain-access-groups</key>
<array /> -->
Expand Down

0 comments on commit be91e33

Please sign in to comment.