Skip to content

Commit

Permalink
Merge branch 'fix/background_stop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Dec 9, 2021
2 parents 1c6598c + d0f8e9b commit 32ce377
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 27 deletions.
70 changes: 70 additions & 0 deletions just_audio_background/.pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#
# Copied from ../.gitignore
#

flutter_export_environment.sh

**/Flutter/App.framework/
**/Flutter/ephemeral/
**/Flutter/Flutter.framework/
**/Flutter/Generated.xcconfig
**/Flutter/flutter_assets/

generated_plugin_registrant.dart
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
GeneratedPluginRegistrant.java
GeneratedPluginRegistrant.swift
build/
.flutter-plugins
coverage
pubspec.lock

#
# Copied from .gitignore
#

# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
4 changes: 4 additions & 0 deletions just_audio_background/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.1-beta.2

* Remove Android notification on stop.

## 0.0.1-beta.1

* Disable next/prev buttons when boundary reached.
Expand Down
4 changes: 2 additions & 2 deletions just_audio_background/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ environment:
dependencies:
flutter:
sdk: flutter
audio_session: ^0.1.5
rxdart: '^0.27.0'
audio_session: ^0.1.6+1
rxdart: ^0.27.2
just_audio:
path: ../../just_audio
just_audio_background:
Expand Down
55 changes: 33 additions & 22 deletions just_audio_background/lib/just_audio_background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ class _JustAudioPlayer extends AudioPlayerPlatform {
final playerDataController = StreamController<PlayerDataMessage>.broadcast();
bool? _playing;
int? _index;
Duration? _duration;
IcyMetadataMessage? _icyMetadata;
int? _androidAudioSessionId;
late final _PlayerAudioHandler _playerAudioHandler;
Expand Down Expand Up @@ -181,7 +180,6 @@ class _JustAudioPlayer extends AudioPlayerPlatform {
});
_audioHandler.mediaItem.listen((mediaItem) {
if (mediaItem == null) return;
_duration = mediaItem.duration;
broadcastPlaybackEvent();
});
}
Expand All @@ -193,10 +191,6 @@ class _JustAudioPlayer extends AudioPlayerPlatform {
await _audioHandler.stop();
}

Future<void> updateQueue(List<MediaItem> queue) async {
await _audioHandler.updateQueue(queue);
}

broadcastPlaybackEvent() {
if (eventController.isClosed) return;
eventController.add(PlaybackEventMessage(
Expand All @@ -213,7 +207,7 @@ class _JustAudioPlayer extends AudioPlayerPlatform {
updateTime: playbackState.updateTime,
bufferedPosition: playbackState.bufferedPosition,
icyMetadata: _icyMetadata,
duration: _duration,
duration: _playerAudioHandler.currentMediaItem?.duration,
currentIndex: _index,
androidAudioSessionId: _androidAudioSessionId,
));
Expand Down Expand Up @@ -383,21 +377,33 @@ class _PlayerAudioHandler extends BaseAudioHandler
.map((event) => TrackInfo(event.currentIndex, event.duration))
.distinct()
.debounceTime(const Duration(milliseconds: 100))
.map((track) {
// Platform may send us a null duration on dispose, which we should
// ignore.
final currentMediaItem = this.currentMediaItem;
if (currentMediaItem != null) {
if (track.duration == null && currentMediaItem.duration != null) {
return TrackInfo(track.index, currentMediaItem.duration);
}
}
return track;
})
.distinct()
.listen((track) {
final currentMediaItem = this.currentMediaItem;
if (currentMediaItem != null) {
if (track.duration != currentMediaItem.duration) {
currentQueue![index!] = currentQueue![index!]
.copyWith(duration: _justAudioEvent.duration);
queue.add(currentQueue!);
}
customEvent.add({
'type': 'currentIndex',
'value': track.index,
if (currentMediaItem != null) {
if (track.duration != currentMediaItem!.duration &&
(index! < queue.nvalue!.length && track.duration != null)) {
currentQueue![index!] =
currentQueue![index!].copyWith(duration: track.duration);
queue.add(currentQueue!);
}
customEvent.add({
'type': 'currentIndex',
'value': track.index,
});
mediaItem.add(currentMediaItem!);
}
});
mediaItem.add(this.currentMediaItem!);
}
});
}

@override
Expand Down Expand Up @@ -606,15 +612,20 @@ class _PlayerAudioHandler extends BaseAudioHandler

@override
Future<void> stop() async {
if (_justAudioEvent.processingState == ProcessingStateMessage.idle) {
return;
}
_updatePosition();
_playing = false;
_broadcastState();
// TODO: We should really stop listening to events here to mimic
// just_audio's behaviour. E.g. if stop() was called, we actually want to
// keep the state around even though the platform may be disposing its own
// state.
_platform.disposePlayer(DisposePlayerRequest(id: (await _player).id));
_justAudioEvent = _justAudioEvent.copyWith(
processingState: ProcessingStateMessage.idle,
);
await _broadcastState();
await super.stop();
}

Duration get currentPosition {
Expand Down
6 changes: 3 additions & 3 deletions just_audio_background/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: just_audio_background
description: An add-on for just_audio that supports background playback and media notifications.
homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_background
version: 0.0.1-beta.1
version: 0.0.1-beta.2

dependencies:
just_audio_platform_interface: ^4.0.0
# just_audio_platform_interface:
# path: ../just_audio_platform_interface
audio_service: ^0.18.0-beta.0
audio_session: ^0.1.5
audio_service: ^0.18.2
audio_session: ^0.1.6+1
flutter:
sdk: flutter
flutter_web_plugins:
Expand Down

0 comments on commit 32ce377

Please sign in to comment.