首页 > 解决方案 > 在 Flutter 中播放自定义声音

问题描述

我正在尝试播放我在资产文件夹中放入应用程序文件夹的自定义 mp3 声音,就像您对字体或图像文件所做的那样,但是我真的不知道如何继续。我想我可能需要将音频文件注册到 pubspec.yaml,但是如何?

我该怎么玩?

我已经检查了这两个答案: 如何在 Flutter 中播放自定义声音?

Flutter - 播放自定义声音更新了吗?

但是第一个太老了,第二个使用URL作为声音路径:const kUrl2 = "http://www.rxlabz.com/labz/audio.mp3";我喜欢播放的声音在应用程序中,而不是在线。所以......我该怎么做?

这是我当前的代码,如您所见,只有一个浮动按钮。我需要从我在代码中声明的那一点开始声音。

但是visual studio用红色的各种部分强调: 等待:它说等待是无法识别的。 audioPlugin.play:是说它也无法识别

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(new MyApp());

Directory tempDir = await getTemporaryDirectory();
File tempFile = new File('${tempDir.path}/demo.mp3');
await tempFile.writeAsBytes(bytes, flush: true);
AudioPlayer audioPlugin = new AudioPlayer();


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


  void _incrementCounter() {
    setState(() {
      print("Button Pressed");

      ///
      /// 
      /// 
      /// Here I Need To start Playing the Sound
      /// 
      /// 
      /// 
      /// 
      audioPlugin.play(tempFile.uri.toString(), isLocal: true);

    });
  }



  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
}

标签: androidaudioflutter

解决方案


它不漂亮,但是...

将 mp3 文件添加到 assets 文件夹并添加到pubspec.yamllike this中。

将资产加载为二进制数据rootBundle.load(asset)

用于path_provider获取应用的临时文件夹位置

使用常规dart:io打开一个文件tempDir(也许使用资产名称)并写入bytes它。

file从表单中的临时文件名形成一个URLfile:///folderPath/fileName

将此传递给audioplayer,设置isLocal为 true (在 iOS 上需要)。

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:audioplayer/audioplayer.dart';
import 'package:path_provider/path_provider.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Audio Player Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

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

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  AudioPlayer audioPlugin = AudioPlayer();
  String mp3Uri;

  @override
  void initState() {
    _load();
  }

  Future<Null> _load() async {
    final ByteData data = await rootBundle.load('assets/demo.mp3');
    Directory tempDir = await getTemporaryDirectory();
    File tempFile = File('${tempDir.path}/demo.mp3');
    await tempFile.writeAsBytes(data.buffer.asUint8List(), flush: true);
    mp3Uri = tempFile.uri.toString();
    print('finished loading, uri=$mp3Uri');
  }

  void _playSound() {
    if (mp3Uri != null) {
      audioPlugin.play(mp3Uri, isLocal: true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Audio Player Demo Home Page'),
      ),
      body: Center(),
      floatingActionButton: FloatingActionButton(
        onPressed: _playSound,
        tooltip: 'Play',
        child: const Icon(Icons.play_arrow),
      ),
    );
  }
}

推荐阅读