Skip to content

Commit

Permalink
Futureproof sinkId, add missing implementations, tests, bump versions…
Browse files Browse the repository at this point in the history
…, CHANGELOGs.
  • Loading branch information
ryanheise committed Feb 1, 2025
1 parent 2d50f59 commit 4ac4ab1
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 29 deletions.
4 changes: 4 additions & 0 deletions just_audio/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.45

* Add setWebSinkId for web (@dganzella).

## 0.9.44

* Add support for SwiftPM.
Expand Down
39 changes: 30 additions & 9 deletions just_audio/lib/just_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class AudioPlayer {
bool _platformLoading = false;
AndroidAudioAttributes? _androidAudioAttributes;
WebCrossOrigin? _webCrossOrigin;
String _webSinkId = '';
final bool _androidApplyAudioAttributes;
final bool _handleAudioSessionActivation;

Expand Down Expand Up @@ -580,6 +581,9 @@ class AudioPlayer {
/// instance on web.
WebCrossOrigin? get webCrossOrigin => _webCrossOrigin;

/// The current sink ID of the `<audio>` element backing this instance on web.
String get webSinkId => _webSinkId;

/// The current position of the player.
Duration get position => _getPositionFor(_playbackEvent);

Expand Down Expand Up @@ -1234,6 +1238,16 @@ class AudioPlayer {
_webCrossOrigin = webCrossOrigin;
}

/// Sets a specific device output id on Web.
Future<void> setWebSinkId(String webSinkId) async {
if (_disposed) return;
if (!kIsWeb && !_isUnitTest()) return;

await (await _platform)
.setWebSinkId(SetWebSinkIdRequest(sinkId: webSinkId));
_webSinkId = webSinkId;
}

/// Release all resources associated with this player. You must invoke this
/// after you are done with the player.
Future<void> dispose() async {
Expand Down Expand Up @@ -1487,10 +1501,17 @@ class AudioPlayer {
? ShuffleModeMessage.all
: ShuffleModeMessage.none));
if (checkInterruption()) return platform;
if (kIsWeb && _webCrossOrigin != null) {
await platform.setWebCrossOrigin(SetWebCrossOriginRequest(
crossOrigin: WebCrossOriginMessage.values[_webCrossOrigin!.index],
));
if (kIsWeb) {
if (_webCrossOrigin != null) {
await platform.setWebCrossOrigin(SetWebCrossOriginRequest(
crossOrigin: WebCrossOriginMessage.values[_webCrossOrigin!.index],
));
}
if (_webSinkId != '') {
await platform.setWebSinkId(SetWebSinkIdRequest(
sinkId: _webSinkId,
));
}
}
for (var audioEffect in _audioPipeline._audioEffects) {
await audioEffect._activate(platform);
Expand Down Expand Up @@ -1528,11 +1549,6 @@ class AudioPlayer {
return durationCompleter.future;
}

/// Sets a specific device output id, null for default
Future<void> setWebSinkId(String? sinkId) async {
await (await _platform).setWebSinkId(sinkId);
}

/// Dispose of the given platform.
Future<void> _disposePlatform(AudioPlayerPlatform platform) async {
if (platform is _IdleAudioPlayer) {
Expand Down Expand Up @@ -3652,6 +3668,11 @@ class _IdleAudioPlayer extends AudioPlayerPlatform {
return SetWebCrossOriginResponse();
}

@override
Future<SetWebSinkIdResponse> setWebSinkId(SetWebSinkIdRequest request) async {
return SetWebSinkIdResponse();
}

@override
Future<SetAutomaticallyWaitsToMinimizeStallingResponse>
setAutomaticallyWaitsToMinimizeStalling(
Expand Down
6 changes: 3 additions & 3 deletions just_audio/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: just_audio
description: A feature-rich audio player for Flutter. Loop, clip and concatenate any sound from any source (asset/file/URL/stream) in a variety of audio formats with gapless playback.
version: 0.9.44
version: 0.9.45
repository: https://github.com/ryanheise/just_audio/tree/minor/just_audio
issue_tracker: https://github.com/ryanheise/just_audio/issues
topics:
Expand All @@ -14,10 +14,10 @@ environment:
flutter: ">=3.10.0"

dependencies:
just_audio_platform_interface: ^4.3.0
just_audio_platform_interface: ^4.4.0
# just_audio_platform_interface:
# path: ../just_audio_platform_interface
just_audio_web: ^0.4.11
just_audio_web: ^0.4.14
# just_audio_web:
# path: ../just_audio_web
audio_session: ^0.1.24
Expand Down
10 changes: 10 additions & 0 deletions just_audio/test/just_audio_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,16 @@ void runTests() {
await player.dispose();
});

test('setWebSinkId', () async {
final player = AudioPlayer();
expect(player.webSinkId, equals(''));
await player.setWebSinkId('foo');
expect(player.webSinkId, equals('foo'));
await player.setWebSinkId('');
expect(player.webSinkId, equals(''));
await player.dispose();
});

test('setAndroidAudioAttributes', () async {
final player = AudioPlayer();
await player.setAndroidAudioAttributes(const AndroidAudioAttributes());
Expand Down
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.15

* Add setWebSinkId for web.

## 0.0.1-beta.14

* Fix shuffleOrder when mutating ConcatenatingAudioSource (@jonmarkhall).
Expand Down
2 changes: 1 addition & 1 deletion just_audio_background/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
dependencies:
flutter:
sdk: flutter
audio_session: ^0.1.23
audio_session: ^0.1.24
rxdart: ^0.27.2
just_audio:
path: ../../just_audio
Expand Down
11 changes: 11 additions & 0 deletions just_audio_background/lib/just_audio_background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ class _JustAudioPlayer extends AudioPlayerPlatform {
return SetWebCrossOriginResponse();
}

@override
Future<SetWebSinkIdResponse> setWebSinkId(SetWebSinkIdRequest request) {
_playerAudioHandler.customSetWebSinkId(request);
throw SetWebSinkIdResponse();
}

@override
Future<SeekResponse> seek(SeekRequest request) =>
_playerAudioHandler.customPlayerSeek(request);
Expand Down Expand Up @@ -519,6 +525,11 @@ class _PlayerAudioHandler extends BaseAudioHandler
return await (await _player).setWebCrossOrigin(request);
}

Future<SetWebSinkIdResponse> customSetWebSinkId(
SetWebSinkIdRequest request) async {
return await (await _player).setWebSinkId(request);
}

Future<ConcatenatingInsertAllResponse> customConcatenatingInsertAll(
ConcatenatingInsertAllRequest request) async {
final cat = _source!.findCat(request.id)!;
Expand Down
4 changes: 2 additions & 2 deletions just_audio_background/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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.14
version: 0.0.1-beta.15
topics:
- audio
- sound
- player
- background

dependencies:
just_audio_platform_interface: ^4.3.0
just_audio_platform_interface: ^4.4.0
# just_audio_platform_interface:
# path: ../just_audio_platform_interface
audio_service: ^0.18.14
Expand Down
4 changes: 4 additions & 0 deletions just_audio_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.4.0

* Add setWebSinkId for web (@dganzella).

## 4.3.0

* Add setWebCrossOrigin for CORS on web (@danielwinkler).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ abstract class AudioPlayerPlatform {
Stream<PlayerDataMessage> get playerDataMessageStream =>
const Stream<PlayerDataMessage>.empty();

/// Sets a specific device output id, null for default
Future<void> setWebSinkId(String? sinkId) {
throw UnimplementedError("setWebSinkId() has not been implemented.");
}

/// Loads an audio source.
Future<LoadResponse> load(LoadRequest request) {
throw UnimplementedError("load() has not been implemented.");
Expand Down Expand Up @@ -241,6 +236,11 @@ abstract class AudioPlayerPlatform {
SetWebCrossOriginRequest request) {
throw UnimplementedError("setWebCrossOrigin() has not been implemented.");
}

/// Sets a specific device output id on the web audio element.
Future<SetWebSinkIdResponse> setWebSinkId(SetWebSinkIdRequest request) {
throw UnimplementedError("setWebSinkId() has not been implemented.");
}
}

/// A data update communicated from the platform implementation to the Flutter
Expand Down Expand Up @@ -1506,3 +1506,11 @@ class SetWebCrossOriginRequest {
class SetWebCrossOriginResponse {}

enum WebCrossOriginMessage { anonymous, useCredentials }

class SetWebSinkIdRequest {
final String sinkId;

SetWebSinkIdRequest({required this.sinkId});
}

class SetWebSinkIdResponse {}
2 changes: 1 addition & 1 deletion just_audio_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A common platform interface for the just_audio plugin. Different pl
homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 4.3.0
version: 4.4.0

dependencies:
flutter:
Expand Down
4 changes: 4 additions & 0 deletions just_audio_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.14

* Add setWebSinkId (@dganzella).

## 0.4.13

* Fix `dart2js`/`dart2wasm` compile error with Flutter 3.26.0 (@SleepySquash).
Expand Down
12 changes: 7 additions & 5 deletions just_audio_web/lib/just_audio_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,6 @@ class Html5AudioPlayer extends JustAudioPlayer {
return orderInv;
}

/// Sets a specific device output id, null for default
Future<void> setWebSinkId(String? sinkId) async {
await _audioElement.setSinkId(sinkId ?? '').toDart;
}

/// Called when playback reaches the end of an item.
Future<void> onEnded() async {
if (_loopMode == LoopModeMessage.one) {
Expand Down Expand Up @@ -364,6 +359,13 @@ class Html5AudioPlayer extends JustAudioPlayer {
return SetWebCrossOriginResponse();
}

/// Sets a specific device output id, null for default
@override
Future<SetWebSinkIdResponse> setWebSinkId(SetWebSinkIdRequest request) async {
await _audioElement.setSinkId(request.sinkId).toDart;
return SetWebSinkIdResponse();
}

@override
Future<SeekResponse> seek(SeekRequest request) async {
await _seek(request.position?.inMilliseconds ?? 0, request.index);
Expand Down
6 changes: 3 additions & 3 deletions just_audio_web/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: just_audio_web
description: Web platform implementation of just_audio. This implementation is endorsed and therefore doesn't require a direct dependency.
homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_web
version: 0.4.13
version: 0.4.14

flutter:
plugin:
Expand All @@ -11,14 +11,14 @@ flutter:
fileName: just_audio_web.dart

dependencies:
just_audio_platform_interface: ^4.3.0
just_audio_platform_interface: ^4.4.0
# just_audio_platform_interface:
# path: ../just_audio_platform_interface
flutter:
sdk: flutter
flutter_web_plugins:
sdk: flutter
web: '>=0.5.1 <2.0.0'
web: ^1.0.0

dev_dependencies:
flutter_lints: ^2.0.1
Expand Down

0 comments on commit 4ac4ab1

Please sign in to comment.