简体   繁体   中英

Unable to play sound with AudioPlayer package

I am trying to play a sound from my asset directory in dart, I have added the required lines of code and also included the asset directory in pubspec.yaml.

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';

void main() {
  return runApp(
    MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: TextButton(
                onPressed: () {
                  AudioPlayer player = AudioPlayer();

                  player.play(DeviceFileSource('note1.wav'));
                },
                child: Text('Click Me'),
            ),
          ),
        ),
      ),
    )
  );
}

Try to change the source of the audio by using AssetSource() and make sure to add the source of your audio in the yaml file

onPressed: () {
    AudioPlayer player = AudioPlayer();
    player.play(AssetSource('audio.mp3'));
},

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