我正在使用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);
}
提前感谢
发布于 2022-04-15 22:21:54
因此,您似乎需要创建自己的类作为StreamAudioSource的扩展。
import 'dart:typed_data';
import 'package:just_audio/just_audio.dart';
class MyJABytesSource extends StreamAudioSource {
final Uint8List _buffer;
MyJABytesSource(this._buffer) : super(tag: 'MyAudioSource');
@override
Future<StreamAudioResponse> request([int? start, int? end]) async {
// Returning the stream audio response with the parameters
return StreamAudioResponse(
sourceLength: _buffer.length,
contentLength: (start ?? 0) - (end ?? _buffer.length),
offset: start ?? 0,
stream: Stream.fromIterable([_buffer.sublist(start ?? 0, end)]),
contentType: 'audio/wav',
);
}
}
然后像这样调用它
await thePlayer.setAudioSource(MyJABytesSource(bytes));
您可以在后面调用thePlayer.play().
,但我更愿意使用它作为侦听器。
thePlayer.processingStateStream.listen((ja.ProcessingState state) {
if (state == ja.ProcessingState.ready) {
// I'm using flutter_cache_manager, and it serves all the file
// under the same name, which is fine, but I think this is why
// I need to pause before I can play again.
// (For tracks after the first, the first plays fine.)
// You probably won't need to pause, but I'm not sure.
thePlayer.pause();
thePlayer.play();
} else if (state == ja.ProcessingState.completed) {
// What to do when it completes.
}
});
这样做的好处是,您实际上不需要await
关键字,这在情景上是有用的。我在这里只想说明这是一个async
函数。
https://stackoverflow.com/questions/67078045
复制相似问题