You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter使用audioplayers包遇到三类技术问题求助

解决Flutter audioplayers本地音频播放问题

问题1:AudioCache导入错误

你使用的audioplayers: ^1.1.1版本中,AudioCache类已整合到主库文件,无需单独导入audio_cache.dart。直接删除该行导入,保留package:audioplayers/audioplayers.dart即可正常使用AudioCache。

问题2:本地MP3文件存放位置

正确位置是项目根目录下创建assets文件夹,将MP3文件放入其中。你的assets/musicForapp.mp3路径是正确的,注意文件名和大小写必须完全匹配(Flutter对路径大小写敏感)。

问题3:pubspec.yaml配置错误

你的assets配置缺少空格,导致Flutter无法识别路径。正确配置需在-后添加空格,推荐直接指定整个assets文件夹(方便后续添加更多音频):

flutter:
  uses-material-design: true
  assets:
    - assets/

若仅需指定单个文件,正确写法为:

assets:
  - assets/musicForapp.mp3

修改后的完整代码

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Time Attack',
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: const Text('Time Attack'),
        centerTitle: true,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          final player = AudioCache();
          player.play('musicForapp.mp3');
        },
        backgroundColor: Colors.red,
        child: const Icon(Icons.punch_clock),
      ),
      body: Padding(
        padding: const EdgeInsets.all(170),
        child: Column(
          children: [
            Container(
              color: Colors.red,
              child: const Text(
                "Click the Icon in the bottom right to simulate an alarm",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 30.0,
                ),
              ),
            ),
            ElevatedButton(
              style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (_) => const TasksPage(),
                  ),
                );
              },
              child: const Text(
                "Navigate",
                style: TextStyle(color: Colors.white),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class TasksPage extends StatefulWidget {
  const TasksPage({super.key});

  @override
  State<TasksPage> createState() => _TasksPageState();
}

class _TasksPageState extends State<TasksPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: const Text('Tasks'),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (_) => const MyHomePage(),
                  ),
                );
              },
              child: const Text(
                "Navigate",
                style: TextStyle(color: Colors.white),
              ),
            )
          ],
        ),
      ),
    );
  }
}

(注:修正了原代码中重复嵌套MaterialApp的问题,每个应用仅需一个根MaterialApp;同时修正了文本拼写错误"bottem"为"bottom",调整了TasksPage中Column的对齐方式)

修改后的pubspec.yaml关键部分

dependencies:
  flutter:
    sdk: flutter
  audioplayers: ^1.1.1
  cupertino_icons: ^1.0.2

flutter:
  uses-material-design: true
  assets:
    - assets/

最后执行flutter pub get更新依赖,重启项目即可正常播放本地音频。

内容的提问来源于stack exchange,提问作者Mozart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 00:45:55