Flutter-AssetsAudioPlayer
Flutter-AssetsAudioPlayer copied to clipboard
[WEB] mp3 from assets failed to load because no supported source was found
Flutter Version Flutter (Channel master, 1.24.0-4.0.pre.98, on Microsoft Windows [Version 10.0.19041.572]) Lib Version assets_audio_player: ^2.0.12 Platform (Android / iOS / web) + version WEB
Describe the bug
When I try to open an MP3 from asset I get the following error message:
Error: NotSupportedError: Failed to load because no supported source was found.
at Object.createErrorWithStack (http://localhost:62567/dart_sdk.js:4352:12)
at Object._rethrow (http://localhost:62567/dart_sdk.js:38191:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:62567/dart_sdk.js:38185:13)
at Object._microtaskLoop (http://localhost:62567/dart_sdk.js:38017:13)
at _startMicrotaskLoop (http://localhost:62567/dart_sdk.js:38023:13)
at http://localhost:62567/dart_sdk.js:33520:9
Works fine on Android though.
I did some digging and it seems the audioElement.play() is the one throwing the exception from javascript althought I would think mp3 is common enough to be a playable source for browsers.
Small code to reproduce
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State {
final AssetsAudioPlayer _assetsAudioPlayer = AssetsAudioPlayer();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
child: Text("open"),
onPressed: () {
final assetsAudioPlayer = AssetsAudioPlayer();
await assetsAudioPlayer.open(
Audio("assets/audio/sound.mp3"),
);
await assetsAudioPlayer.play();
}
),
),
),
);
}
}
I'm having the same issue when trying to play mp3 files from assets on Flutter WEB, Channel beta, 1.25.0-8.1.pre.
Is there some workaround or do I have to refrain from using it until it is fixed?
same on Audio.file()
To work on Flutter web I only managed it like this:
await assetsAudioPlayer.open( Audio.network("assets/sounds/simple-notification.mp3"), showNotification: true, );
Basically replaces Audio(assetHere) with Audio.network(assetHere), it seems that Flutter web has some strict policies on accessing local files directly.
To work on Flutter web I only managed it like this:
await assetsAudioPlayer.open( Audio.network("assets/sounds/simple-notification.mp3"), showNotification: true, );
Basically replaces Audio(assetHere) with Audio.network(assetHere), it seems that Flutter web has some strict policies on accessing local files directly.
Works for me