英文:
Justaudio Flutter cant run
问题
onTap: () {
final player = AudioPlayer();
player.play(
player.setAudioSource('assets/sound.wav'),
);
}
There was an error in my audio playback in Flutter, so I can't play the audio. Please help me.
I created my code in VSCode and added the asset to my pubspec.yaml
file and imported the package.
<details>
<summary>英文:</summary>
onTap: () {
final player = AudioPlayer();
player.play(
player.setAudioSource('assets/sound.wav'),
);
There was an error in my just auido flutter so I can't run the audio. help me please.
I create my code in vscode and i had add asset in public yaml and import the package.
</details>
# 答案1
**得分**: 0
[setAudioSource][1] 需要传递一个 [AudioSource][2]。你正在传递一个字符串。
相反,你可以使用带有字符串参数的 [setAsset][3],它会负责创建 AudioSource 并为你调用 setAudioSource。
onTap: () {
final player = AudioPlayer();
player.setAsset('assets/sound.wav').then(() => {
player.play();
}),
},
请注意,每次点击都创建一个新的 AudioPlayer 实例不是一个好主意,你应该只创建一次播放器并重复使用它。
[1]: https://pub.dev/documentation/just_audio/latest/just_audio/AudioPlayer/setAudioSource.html
[2]: https://pub.dev/documentation/just_audio/latest/just_audio/AudioSource-class.html
[3]: https://pub.dev/documentation/just_audio/latest/just_audio/AudioPlayer/setAsset.html
<details>
<summary>英文:</summary>
[setAudioSource][1] expects to be passed an [AudioSource][2]. You are passing it a string.
Instead you can use [setAsset][3] with the string parameter, which will take care of creating the AudioSource and calling setAudioSource for you.
onTap: () {
final player = AudioPlayer();
player.setAsset('assets/sound.wav').then( () => {
player.play();
} ),
},
Please note that creating a new AudioPlayer instance on every tap is not a good idea, instead you should create the player just once and re-use it.
[1]: https://pub.dev/documentation/just_audio/latest/just_audio/AudioPlayer/setAudioSource.html
[2]: https://pub.dev/documentation/just_audio/latest/just_audio/AudioSource-class.html
[3]: https://pub.dev/documentation/just_audio/latest/just_audio/AudioPlayer/setAsset.html
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论