Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions packages/audioplayers_web/lib/wrapped_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class WrappedPlayer {
bool _isPlaying = false;

web.HTMLAudioElement? player;
web.AudioContext? _audioContext;
web.MediaElementAudioSourceNode? _sourceNode;
web.StereoPannerNode? _stereoPanner;
StreamSubscription? _playerEndedSubscription;
StreamSubscription? _playerLoadedDataSubscription;
Expand Down Expand Up @@ -79,12 +81,14 @@ class WrappedPlayer {

_setupStreams(p);

// setup stereo panning
final audioContext = web.AudioContext();
final source = audioContext.createMediaElementSource(player!);
_stereoPanner = audioContext.createStereoPanner();
// Reuse or create AudioContext (Safari limits concurrent contexts)
_audioContext ??= web.AudioContext();

final source = _audioContext!.createMediaElementSource(p);
_sourceNode = source;
_stereoPanner = _audioContext!.createStereoPanner();
source.connect(_stereoPanner!);
_stereoPanner?.connect(audioContext.destination);
_stereoPanner?.connect(_audioContext!.destination);

// Preload the source
p.load();
Expand Down Expand Up @@ -171,6 +175,8 @@ class WrappedPlayer {

void release() {
pause();
_sourceNode?.disconnect();
_sourceNode = null;
// Release `AudioElement` correctly (#966)
player?.src = '';
player?.remove();
Expand All @@ -197,6 +203,12 @@ class WrappedPlayer {
if (player == null) {
recreateNode();
}

// Safari requires explicit resume after user gesture
if (_audioContext != null && _audioContext!.state == 'suspended') {
await _audioContext!.resume().toDart;
}

player?.currentTime = position;
await player?.play().toDart;
}
Expand Down Expand Up @@ -240,6 +252,8 @@ class WrappedPlayer {

Future<void> dispose() async {
release();
await _audioContext?.close().toDart;
_audioContext = null;
eventStreamController.close();
}
}