Justaudio Flutter无法运行。

huangapple go评论51阅读模式
英文:

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&#39;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(&#39;assets/sound.wav&#39;).then( () =&gt; {
            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>



huangapple
  • 本文由 发表于 2023年7月18日 10:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定