android - 在 Flutter 中播放自定义声音
问题描述
我正在尝试播放我在资产文件夹中放入应用程序文件夹的自定义 mp3 声音,就像您对字体或图像文件所做的那样,但是我真的不知道如何继续。我想我可能需要将音频文件注册到 pubspec.yaml,但是如何?
我该怎么玩?
我已经检查了这两个答案: 如何在 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),
),
);
}
}
解决方案
它不漂亮,但是...
将 mp3 文件添加到 assets 文件夹并添加到pubspec.yaml
like 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),
),
);
}
}
推荐阅读
- kubernetes - 如何检查 K8s 卷是否安装正确?
- javascript - 只显示点击标题的内容并隐藏其他
- youtube-api - Youtube v3 playlistItems.insert api 返回 Video not found 404 错误,但视频已发布在正确的播放列表中
- floating-point - IEEE 单一格式的整数与浮点数
- pandas - 在 spark 数据帧上实现 pythonic 统计函数
- rust - 在 rust 中使用 reqwest 获取响应标头值
- c# - ASP .NET core 3.0 依赖注入抛出 System.InsufficientExecutionStackException
- c# - 设置 CheckBoxes isChecked 默认 WPF MVVM (System.NullReferenceException)
- ruby-on-rails - 使用载波将图像保存在 ruby on rails 中的问题并且反应图像值为空
- r - tidyverse:有效地绑定列表元素