简体   繁体   中英

Audio does not play - Flutter

I am new to coding and flutter. Trying to figure out why the audio not playing on pressed button. No errors in the console. pub.yaml is ok too:

flutter:

  uses-material-design: true

  assets:
    - assets/
    - assets/audios/
    - assets/audios/assets_note1.wav

Here is main.dart:

import 'package:ocarina/ocarina.dart';

void main() {
  runApp(const ElySays());
}

class ElySays extends StatelessWidget {
  const ElySays({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    onPressed: () async {
                      final player = OcarinaPlayer(
                        asset: 'assets/audios/assets_note1.wav',
                        package: 'elysays',
                        loop: true,
                        volume: 1.0,
                      );

                      await player.load();
                      print(Text('Pressed'));
                    },
                    child: Text(
                      'Press me!',
                      style: TextStyle(fontSize: 30), //just text
                    ), //just text
                  ), //just text
                ), //just text
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Second day trying to understand what is the problem and I had to type more to make a post, so I do not know what I did wrong.

You have to call the player.play(); method to play the audio when the button is pressed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM