Code Monkey home page Code Monkey logo

Comments (3)

novas1r1 avatar novas1r1 commented on May 25, 2024 2

Hey,
I had the same initializing error and with your new version it worked :)!
Thanks for the fast fix!

from flutter_soloud.

alnitak avatar alnitak commented on May 25, 2024 1

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 call init() just after instanting for the first time SoLoudImpl, it will not work.
  • on dispose() you'd better call deinit() instead disposeAllSources() (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.

alnitak avatar alnitak commented on May 25, 2024 1

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.