5

我正在使用 just_audio 插件,它描述了一个功能:从字节流中读取。

基本上,当我播放一个文件(来自 url)时,我正在保存文件中的字节,所以在这一步之后我想在本地播放它。

我有一个关于如何从字节流播放的问题。谁能提供一个例子如何做到这一点?我需要把它放在我的播放列表中,所以它必须是 ConcatanatingAudioSource 的子级。

我发现的唯一音频源是从 Uri 使用它。

final _playlist = ConcatenatingAudioSource(
children: [
    AudioSource.uri(
      Uri.parse(
          "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"),
      tag: AudioMetadata(
        album: "Science Friday",
        title: "ddddd",
        artwork:
            "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
      ),
    )
]
)

这就是我保存字节的方式:

void getBytes() async {
  Uri uri = Uri.parse(url);
  var rng = new Random();
// get temporary directory of device.
  Directory tempDir = await getTemporaryDirectory();
// get temporary path from temporary directory.
  String tempPath = tempDir.path;
// create a new file in temporary path with random file name.
  File file = new File('$tempPath' + (rng.nextInt(100)).toString() + '.mp3');
// call http.get method and pass imageUrl into it to get response.
  http.Response response = await http.get(uri);
// write bodyBytes received in response to file.
  await file.writeAsBytes(response.bodyBytes);
}

提前致谢

4

0 回答 0