简体   繁体   English

无法使用 AudioPlayer package 播放声音

[英]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.我正在尝试从 dart 中的资产目录播放声音,我添加了所需的代码行,并将资产目录包含在 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尝试使用AssetSource()更改音频源,并确保在 yaml 文件中添加音频源

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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