gpt4 book ai didi

android - 在 Flutter 中播放自定义声音

转载 作者:IT王子 更新时间:2023-10-29 07:06:24 30 4
gpt4 key购买 nike

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

我该如何玩?

我已经检查了这两个答案: How to play a custom sound in Flutter?

Flutter - Play custom sounds updated?

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

这是我当前的代码,如您所见,只有一个 float 按钮。我需要在我在代码中声明的位置开始播放声音。

但 visual studio 以红色下划线显示各个部分:await:表示无法识别await。audioPlugin.play: 是说 It's also unrecognized

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喜欢this .

使用 rootBundle.load(asset) 将 Assets 作为二进制数据加载

使用 path_provider获取应用程序的临时文件夹位置

使用正则 dart:iotempDir 中打开文件(也许使用 Assets 名称)并写 bytes

形成一个file来自临时文件名的 URL,格式为 file:///folderPath/fileName

将此传递给 audioplayer , 设置 isLocal为真(在 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),
),
);
}
}

关于android - 在 Flutter 中播放自定义声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51901558/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com