颤振:按下按钮时播放声音

问题描述:

我对编程非常陌生,尤其是颤抖.我想写一个音板,基本上是一个按钮列表,每个按钮的工作是播放我在PC上按下时发出的声音.我已经使用列表视图和一些素材按钮完成了设计,但是我不知道如何在按下它们时使它们播放声音.有人可以帮我吗?

I'm very new to programing and especially flutter. I want to write an soundboard, basically a list of buttons and the job of each button is to play a sound that i have on my PC when they are pressed. I've already done the design with a list view and some material buttons but I don't know how to make them play sounds when I press them. Can some one help me?

完整代码

import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';


void main() => runApp(HomePage());

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  AudioCache _audioCache;

  @override
  void initState() {
    super.initState();
    // create this only once
    _audioCache = AudioCache(prefix: "audio/", fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("Music play")),
        body: RaisedButton(
          onPressed: () => _audioCache.play('my_audio.mp3'),
          child: Text("Play Audio"),
        ),
      ),
    );
  }
}


  1. 在您的 pubspec.yaml 文件中添加依赖项, audioplayers:any .

在您的 pubspec.yaml 文件中添加音频文件.

Add audio file in your pubspec.yaml file.

资产:-资产/audio/my_audio.mp3

assets: - assets/audio/my_audio.mp3

运行 flutter软件包获取