Comments (3)
Hey,
I had the same initializing error and with your new version it worked :)!
Thanks for the fast fix!
from flutter_soloud.
Hi @daniloapr, thanks for your findings!
I did some tests and figured out that there was a problem when doing a hot reload restart. This is a complete minimal example done with your code:
code:
import 'dart:developer' as dev;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_soloud/flutter_soloud.dart';
import 'package:logging/logging.dart';
void main() async {
Logger.root.level = kDebugMode ? Level.FINE : Level.INFO;
Logger.root.onRecord.listen((record) {
dev.log(
record.message,
time: record.time,
level: record.level.value,
name: record.loggerName,
zone: record.zone,
error: record.error,
stackTrace: record.stackTrace,
);
});
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late AudioSource sound;
final impl = SoLoudImpl.instance;
@override
void dispose() {
impl.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
themeMode: ThemeMode.dark,
darkTheme: ThemeData.dark(useMaterial3: true),
home: Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
OutlinedButton(
onPressed: impl.init,
child: const Text('init'),
),
OutlinedButton(
onPressed: impl.playBeep,
child: const Text('beep'),
),
],
),
),
),
);
}
}
class SoLoudImpl {
SoLoudImpl._() {
try {
/// You should fix this because the `init()` must be awaited
player.init();
} catch (e) {
debugPrint(e.toString());
}
}
static final SoLoudImpl instance = SoLoudImpl._();
final player = SoLoud.instance;
AudioSource? beep;
Future<void> init() async {
try {
beep = await player.loadAsset('assets/audio/explosion.mp3');
} catch (e) {
debugPrint(e.toString());
}
}
void dispose() {
/// Instead of `disposeAllSources()` I would use `deinit()` which also disposes all sources
// player.disposeAllSources();
player.deinit();
}
void _play(AudioSource? source) {
assert(source != null, 'AudioSource not initialized');
if (source != null) {
player.play(source);
}
}
void playBeep() {
_play(beep);
}
}
A side note on the code:
- you must await
player.init();
to catch error and to be sure all gone well. For example if you callinit()
just after instanting for the first timeSoLoudImpl
, it will not work. - on
dispose()
you'd better calldeinit()
insteaddisposeAllSources()
(just by intuition from the method name).
I did a change in the plugin. Can you please try it and tell me if it works for you? You can try by replacing
dependencies:
flutter_soloud: ^2.0.0
with
dependencies:
flutter_soloud:
git:
url: https://github.com/alnitak/flutter_soloud.git
ref: dev
from flutter_soloud.
I have pushed version 2.0.1 with the fix for this issue. Also, thanks @novas1r1 for the feedback.
from flutter_soloud.
Related Issues (20)
- feat: Add custom source data for capturing HOT 3
- fix: Crash when opening a malformed mp3 file HOT 2
- feat: Make it possible to "instance" SoLoud? HOT 3
- feat: Find a better mechanism than the `audioEvent` stream for listening to changes in the engine HOT 3
- feat: Fail-fast more often HOT 2
- feat: WASM support HOT 27
- chore: Address C++ compilation warnings HOT 2
- proposal: Another set of renames HOT 4
- feat: Implement setProtectVoice() HOT 1
- docs: What does disposeSound actually do? HOT 2
- fix: Wrong error reported when trying to add a filter twice
- bug: crash on deinit() HOT 1
- fix: App asks for microphone permission even though it's not using it HOT 4
- feat: Simplified Recording Mechanism HOT 12
- feat: WaveForm for complete Audiofile HOT 1
- perf: Ability to play precise sound sequences HOT 3
- fix: Missing audio on playing a loaded url HOT 1
- fix: handles become invalid at unknown points HOT 9
- MA_NODE_FLAG_SILENT_OUTPUT duplicates API recursive triggers (bangs) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_soloud.