本文介绍了如何在Fighter中播放音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Ffltter开发的新手,我陷入了一个程序中,我想在按下TextButton()时播放音频文件,但在按下该按钮时没有播放音频,并且在调试控制台中收到了一条巨大的消息。有谁能帮我检测一下这个问题吗?
这是我在调试控制台中收到的消息:
Launching libmain.dart on Mi A2 in debug mode...
libmain.dart:1
√ Built buildappoutputsflutter-apkapp-debug.apk.
Connecting to VM Service at ws://127.0.0.1:52093/iUDgc5cZVsg=/ws
W/ample.xylophon(10468): Accessing hidden method Lsun/misc/Unsafe;->getUnsafe()Lsun/misc/Unsafe; (greylist,core-platform-api, linking, allowed)
W/ample.xylophon(10468): Accessing hidden method Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform-api, linking, allowed)
W/ample.xylophon(10468): Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed)
W/ample.xylophon(10468): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/ample.xylophon(10468): Accessing hidden method Landroid/media/AudioTrack;->getLatency()I (greylist, reflection, allowed)
I/ExoPlayerImpl(10468): Init 2f4b4e0 [ExoPlayerLib/2.13.1] [jasmine_sprout, Mi A2, Xiaomi, 29]
W/ample.xylophon(10468): Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed)
V/AudioTrack(10468): getMinFrameCount(): getMinFrameCount=3536: afFrameCount=1920, afSampleRate=48000, afLatency=80
V/AudioTrack(10468): set(): streamType -1, sampleRate 44100, format 0x1, channelMask 0x3, frameCount 14144, flags #0, notificationFrames 0, sessionId 24825, transferType 3, uid -1, pid -1
V/AudioTrack(10468): set(): streamType -1 frameCount 14144 flags 0000
V/AudioTrack(10468): set(): Building AudioTrack with attributes: usage=1 content=0 flags=0xa00 tags=[]
这是我的代码:
import 'package:just_audio/just_audio.dart';
import 'package:flutter/material.dart';
void main() {
runApp(Xylophone());
}
class Xylophone extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: TextButton(onPressed: ()async{
final player = AudioPlayer();
await player.setAsset('assets/assets_note1.wav');
},
child:Text('Click me'),
),
),
),
),
);
}
}
推荐答案
将此依赖项添加到pubspec.yaml文件Assets_AUDIO_Player:^3.0.3+3
创建playSound函数,如
playSound() async {
AssetsAudioPlayer audioPlayer = AssetsAudioPlayer();
audioPlayer.open(Audio('assets/bells.mp3'));
}
内部小工具创建按钮
TextButton(
onPressed: () => playSound(),
child: Text('Play'),
),
如果出现某些错误,请转到我们的项目目录androidappuild.gradle,并更改编译SdkVersion 28、minSdkVersion 21、
目标SdkVersion 30
它解决了您问题
这篇关于如何在Fighter中播放音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!