flutter 如何将ConcatenatingAudioSource更改为foreach循环

lb3vh1jj  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(130)

我想动态地使用for循环从资产或目录连接音频源,而不是通过显示所有音频源进行硬编码。
下面的代码显示所有的音频源:

final _playlist = ConcatenatingAudioSource(children: [
  AudioSource.uri(
    Uri.parse(
        "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"),
    tag: AudioMetadata(
      album: "Science Friday",
      title: "A Salute To Head-Scratching Science",
      artwork:
      "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
    ),
  ),
  AudioSource.uri(
   Uri.parse("https://s3.amazonaws.com/scifri-segments/scifri201711241.mp3"),
    tag: AudioMetadata(
      album: "Science Friday",
      title: "From Cat Rheology To Operatic Incompetence",
      artwork:
      "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
    ),
  ),
  AudioSource.uri(
    Uri.parse("asset:///audio/nature.mp3"),
    tag: AudioMetadata(
      album: "Public Domain",
      title: "Nature Sounds",
      artwork:
      "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
    ),
  ),
]);

那么,如何简化代码,使其只显示一个使用foreach循环的变量AudioSource.uri(....)呢?

jgzswidk

jgzswidk1#

“ConcatenatingAudioSource”下的“children”接受一个列表。您可以创建一个单独的函数,该函数循环访问上面的现有值或从其他位置访问的值,并返回一个列表,然后可以将该列表传递给“children”。这样,您就可以得到如下所示的内容:

final _playlist = ConcatenatingAudioSource(children: yourFunction() )

相关问题