Skip to content

Commit

Permalink
feat(web): Support compilation to Wasm (#1766)
Browse files Browse the repository at this point in the history
# Description

This PR migrates usage of `dart:html` and `package:js` to their new
replacements. It also updates version constraints and CI configuration
to account for the versions required. These changes enable compilation
to Wasm and improves compatibility with Dart's evolving web interop
story. There are potentially bundle size improvements as well.

Dart and Flutter will prefer `dart:js_interop`, extension types, and
`package:web` as used here going forward. Check out
https://dart.dev/interop/js-interop/package-web to learn more.

Fixes #1765
  • Loading branch information
parlough authored Mar 13, 2024
1 parent 81e5ea5 commit 1b1a0cf
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 73 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
type: choice
options:
- 'any'
- '3.19.x'
- '3.16.x'
- '3.13.x'
flutter_channel:
Expand Down Expand Up @@ -60,7 +61,7 @@ on:
inputs:
flutter_version:
required: false
default: '3.16.8'
default: '3.19.3'
type: string
flutter_channel:
required: false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ jobs:
call-min-flutter-test:
uses: ./.github/workflows/test.yml
with:
flutter_version: '3.13.0'
flutter_version: '3.19.3'
fatal_warnings: false
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
type: choice
options:
- 'any'
- '3.19.x'
- '3.16.x'
- '3.13.x'
flutter_channel:
Expand Down Expand Up @@ -60,7 +61,7 @@ on:
inputs:
flutter_version:
required: false
default: '3.16.8'
default: '3.19.3'
type: string
flutter_channel:
required: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Future<void> testStreamsTab(
await tester.pumpAndSettle();

// Stream position is tracked as soon as source is loaded
if (!audioSourceTestData.isLiveStream) {
// FIXME: Flaky position test for web, remove kIsWeb check.
if (!kIsWeb && !audioSourceTestData.isLiveStream) {
// Display position before playing
await tester.testPosition(Duration.zero);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/audioplayers/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies:
file_picker: ^6.1.1
flutter:
sdk: flutter
http: '>=0.13.1 <2.0.0'
http: ^1.0.0
path_provider: ^2.0.12
provider: ^6.0.5

Expand Down
55 changes: 0 additions & 55 deletions packages/audioplayers_web/lib/web_audio_js.dart

This file was deleted.

23 changes: 13 additions & 10 deletions packages/audioplayers_web/lib/wrapped_player.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:async';
import 'dart:html';
import 'dart:js_interop';

import 'package:audioplayers_platform_interface/audioplayers_platform_interface.dart';
import 'package:audioplayers_web/num_extension.dart';
import 'package:audioplayers_web/web_audio_js.dart';
import 'package:flutter/services.dart';
import 'package:web/web.dart' as web;

class WrappedPlayer {
final String playerId;
Expand All @@ -17,8 +17,8 @@ class WrappedPlayer {
String? _currentUrl;
bool _isPlaying = false;

AudioElement? player;
StereoPannerNode? _stereoPanner;
web.HTMLAudioElement? player;
web.StereoPannerNode? _stereoPanner;
StreamSubscription? _playerEndedSubscription;
StreamSubscription? _playerLoadedDataSubscription;
StreamSubscription? _playerPlaySubscription;
Expand Down Expand Up @@ -61,11 +61,14 @@ class WrappedPlayer {
}

void recreateNode() {
if (_currentUrl == null) {
final currentUrl = _currentUrl;
if (currentUrl == null) {
return;
}

final p = player = AudioElement(_currentUrl);
final p = player = web.HTMLAudioElement();
p.preload = 'auto';
p.src = currentUrl;
// As the AudioElement is created dynamically via script,
// features like 'stereo panning' need the CORS header to be enabled.
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Expand All @@ -77,7 +80,7 @@ class WrappedPlayer {
_setupStreams(p);

// setup stereo panning
final audioContext = JsAudioContext();
final audioContext = web.AudioContext();
final source = audioContext.createMediaElementSource(player!);
_stereoPanner = audioContext.createStereoPanner();
source.connect(_stereoPanner!);
Expand All @@ -87,7 +90,7 @@ class WrappedPlayer {
p.load();
}

void _setupStreams(AudioElement p) {
void _setupStreams(web.HTMLAudioElement p) {
_playerLoadedDataSubscription = p.onLoadedData.listen(
(_) {
eventStreamController.add(
Expand Down Expand Up @@ -140,7 +143,7 @@ class WrappedPlayer {
_playerErrorSubscription = p.onError.listen(
(_) {
String platformMsg;
if (p.error is MediaError) {
if (p.error != null) {
platformMsg = 'Failed to set source. For troubleshooting, see '
'https://github.com/bluefireteam/audioplayers/blob/main/troubleshooting.md';
} else {
Expand Down Expand Up @@ -195,7 +198,7 @@ class WrappedPlayer {
recreateNode();
}
player?.currentTime = position;
await player?.play();
await player?.play().toDart;
}

Future<void> resume() async {
Expand Down
6 changes: 3 additions & 3 deletions packages/audioplayers_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ dependencies:
sdk: flutter
flutter_web_plugins:
sdk: flutter
js: ^0.6.4
web: ^0.5.1

dev_dependencies:
flame_lint: ^1.0.0
flutter_test:
sdk: flutter

environment:
sdk: '>=3.0.0 <4.0.0'
flutter: '>=3.13.0'
sdk: '>=3.3.0 <4.0.0'
flutter: '>=3.19.0'

0 comments on commit 1b1a0cf

Please sign in to comment.