使用AudioPlayers从按钮中播放单个音符

发布于 2025-02-14 01:58:04 字数 721 浏览 0 评论 0原文

我正在对AudioPlayers软件包进行过时的教程,并尝试在按下按钮时播放单个音符。我无法让它上​​班,有人可以帮忙吗

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

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

class XylophoneApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: TextButton(
              onPressed: () {
                final player = AudioCache();
                player.play('note1.wav'); //ERROR THAT 'play' method is not valid
              },
              child: Text('Click Me'),
            ),
          ),
        ),
      ),
    );
  }
}

I am doing an outdated tutorial on the audioplayers package and just trying to play a single note from when the button is pressed. I am not able to get it to work, can someone please help

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

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

class XylophoneApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: TextButton(
              onPressed: () {
                final player = AudioCache();
                player.play('note1.wav'); //ERROR THAT 'play' method is not valid
              },
              child: Text('Click Me'),
            ),
          ),
        ),
      ),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2025-02-21 01:58:04

为了从资产中播放声音,您需要一个audioplayer并将资产设置为其。

onPressed: () async {
    final player = AudioPlayer();
    await player.setAsset('note1.wav'); // make sure to add on pubspec.yaml and provide correct path
    player.play();
  }

In order to play sound from assets, you need a AudioPlayer and set asset to it.

onPressed: () async {
    final player = AudioPlayer();
    await player.setAsset('note1.wav'); // make sure to add on pubspec.yaml and provide correct path
    player.play();
  }
喵星人汪星人 2025-02-21 01:58:04

终于找到了修复程序,问题与新版本的听力播放器有关,而无需导入各种功能

onPressed: () {
   final player = AudioPlayer();
   player.play(AssetSource("note1.wav"));
}

Finally found the fix, the problem all had to do with the new version of audioplayers having all the functions in one class without having to import seperatly audiocache

onPressed: () {
   final player = AudioPlayer();
   player.play(AssetSource("note1.wav"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文