Skip to content

Commit 8ddc512

Browse files
committed
Add support for local assets on web
1 parent 4ceb087 commit 8ddc512

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/audio_cache.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22
import 'dart:io';
33
import 'dart:typed_data';
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/services.dart' show rootBundle;
67
import 'package:path_provider/path_provider.dart';
78

@@ -99,10 +100,10 @@ class AudioCache {
99100
bool isNotification,
100101
PlayerMode mode = PlayerMode.MEDIA_PLAYER,
101102
bool stayAwake}) async {
102-
File file = await load(fileName);
103+
String url = await getAbsaoluteUrl(fileName);
103104
AudioPlayer player = _player(mode);
104105
await player.play(
105-
file.path,
106+
url,
106107
volume: volume,
107108
respectSilence: isNotification ?? respectSilence,
108109
stayAwake: stayAwake,
@@ -120,15 +121,23 @@ class AudioCache {
120121
bool isNotification,
121122
PlayerMode mode = PlayerMode.MEDIA_PLAYER,
122123
bool stayAwake}) async {
123-
File file = await load(fileName);
124+
String url = await getAbsaoluteUrl(fileName);
124125
AudioPlayer player = _player(mode);
125126
player.setReleaseMode(ReleaseMode.LOOP);
126127
player.play(
127-
file.path,
128+
url,
128129
volume: volume,
129130
respectSilence: isNotification ?? respectSilence,
130131
stayAwake: stayAwake,
131132
);
132133
return player;
133134
}
135+
136+
Future<String> getAbsaoluteUrl(String fileName) async {
137+
if (kIsWeb) {
138+
return "assets/assets/$prefix$fileName";
139+
}
140+
File file = await load(fileName);
141+
return file.path;
142+
}
134143
}

0 commit comments

Comments
 (0)