首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤振just_audio包如何从字节播放音频

颤振just_audio包如何从字节播放音频
EN

Stack Overflow用户
提问于 2021-04-13 15:37:18
回答 1查看 2.2K关注 0票数 6

我正在使用just_audio插件,它在描述中有一个特性:从字节流读取。

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

我有一个关于如何从字节流中播放的问题。有人能举个例子来说明如何做到这一点吗?我需要把这个放到我的播放列表上,所以它必须是ConcatanatingAudioSource的孩子。

我找到的唯一的音频源是从Uri中使用它。

代码语言:javascript
运行
复制
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",
      ),
    )
]
)

我就是这样保存字节的:

代码语言:javascript
运行
复制
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);
}

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2022-04-15 22:21:54

因此,您似乎需要创建自己的类作为StreamAudioSource的扩展。

代码语言:javascript
运行
复制
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',
    );
  }
}

然后像这样调用它

代码语言:javascript
运行
复制
await thePlayer.setAudioSource(MyJABytesSource(bytes));

您可以在后面调用thePlayer.play().,但我更愿意使用它作为侦听器。

代码语言:javascript
运行
复制
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函数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67078045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档