dart 如何在Flutter中使用本地资源播放音频?

0pizxfdo  于 2023-02-14  发布在  Flutter
关注(0)|答案(2)|浏览(312)

所以我使用Flutter包audioplayers在我的应用程序上播放音频,但问题是导入包后,它说找不到play()函数。
下面是代码:

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: TextButton(
              child: const Text('Click Me'),
              onPressed: () {
                final player = AudioCache();
                player.play("note1.wav"); // Here it says that the play function is not defined
              },
            ),
          ),
        ),
      ),
    );
  }
}
ergxz8rk

ergxz8rk1#

下面的代码不再有效

final player = AudioCache();
player.play("note1.wav");

您可以执行以下操作,而不是上面的代码

final player = AudioPlayer();
player.play(UrlSource('note1.wave'));

// If file located in assets folder like assets/sounds/note01.wave"
await player.play(AssetSource('sounds/note1.wave'));

Source

a8jjtwal

a8jjtwal2#

按下时:(){音频播放器播放器=音频播放器();(资产来源("注1.wav"));player.play(AssetSource("note1.wav")); },

相关问题