Search code examples
flutterflutter-packages

I want to add background audio in my splash screen. So tried the below code. But I got some exceptions. ( Flutter )


I want to add background audio in my splash screen. So tried the below code. But I got some exceptions. Anyone pls help me to solve this.Here I use the package Flutter Sound.

-------------This is My Code-----------------

import 'dart:async';
import 'package:FlutterNewApp/Screens/HomeMain.dart';
import 'package:flutter/material.dart';
import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter_sound/flutter_sound.dart';
class Splash extends StatefulWidget {
  @override
  _SplashState createState() => _SplashState();
}
class _SplashState extends State<Splash> {
  @override
  void initState() {
    FlutterSound flutterSound = FlutterSound();
    flutterSound.thePlayer.startPlayer(fromURI: 'assets/audio/splash.mp3');//My splash.mp3 is inside the audio folder in assets folder    
    super.initState();
    Timer(
        Duration(minutes: 50),
        () => Navigator.pushReplacement(
            context, MaterialPageRoute(builder: (context) => HomeMain())));
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
        Center(
          child: AnimatedTextKit(
            totalRepeatCount: 1,
            //repeatForever: true,
            animatedTexts: [
            TyperAnimatedText(
              'Tofee Ride',
              //speed: Duration(milliseconds: 50),
              textStyle: TextStyle(
                fontFamily: 'Ultra-Regular',
                color: Colors.pink[600],
                fontSize: 30,
              )
              ),  
          ]),
        ),
        Center
        (child:AnimatedTextKit(
          totalRepeatCount: 1,
          //repeatForever: true,
          animatedTexts: [
          TyperAnimatedText('Learning App for classes I - IV')
        ]) ,)
      ],)
    );
  }
}

Exception is given below. Pls Help. Here I use the Package Flutter Sound.

This is the Exception I got


Solution

  • As explained in the exception message: "Player is not open"

    Flutter_sound documentation say:

    1. Open and close the audio session

    Before calling startPlayer() you must open the Session.

    When you have finished with it, you must close the session. A good places to put those verbs are in the procedures initState() and dispose().

    @override
      void initState() {
        super.initState();
        // Be careful : openAudioSession return a Future.
        // Do not access your FlutterSoundPlayer or FlutterSoundRecorder before the completion of the Future
        _myPlayer.openAudioSession().then((value) {
          setState(() {
            _mPlayerIsInited = true;
          });
        });
      }
    
    
    
      @override
      void dispose() {
        // Be careful : you must `close` the audio session when you have finished with it.
        _myPlayer.closeAudioSession();
        _myPlayer = null;
        super.dispose();
      }
    
    

    https://tau.canardoux.xyz/guides_getting_started.html#2-open-and-close-the-audio-session