Flutter音频合成与播放插件dart_sunvox的使用
Flutter音频合成与播放插件dart_sunvox的使用
Dart FFI绑定用于SunVox库。
功能
查看 SunVox库网站。
入门指南
你需要从其网页上获取适用于你操作系统的LibSunvox共享库,并在LibSunvox构造函数中传递共享库文件的路径作为第二个参数。
使用方法
查看/example
文件夹中的示例。
final sunvox = LibSunvox(0, "./sunvox.so");
const filename = "sunvox_lib/resources/song01.sunvox";
await sunvox.load(filename);
sunvox.volume = 256;
sunvox.play();
print("playing:$filename ...");
await Future<void>.delayed(Duration(seconds: 5));
sunvox.stop();
sunvox.shutDown();
开发
要重新生成Dart FFI绑定文件,请运行:
dart run ffigen
许可证
Dart绑定的许可证位于本仓库的LICENSE
文件中。有关SunVox库内包含代码的许可信息,请参阅sunvox_lib/docs/license
。
示例代码
import 'dart:async';
import 'dart:io';
import 'package:dart_sunvox/dart_sunvox.dart';
void main(List<String> args) async {
final sunvox = LibSunvox(0);
final v = sunvox.versionString();
print('sunvox lib version: $v');
const filename = "sunvox_lib/resources/song01.sunvox";
await sunvox.load(filename);
// 或者使用Dart的文件操作读取数据
// final data = File(filename).readAsBytesSync();
sunvox.volume = 256;
sunvox.play();
print("playing:$filename ...");
int _lastLine = 0;
Timer.periodic(Duration(milliseconds: 50), (timer) {
final line = sunvox.currentLine;
if (_lastLine != line) {
_lastLine = line;
print("line: $_lastLine");
}
});
await Future<void>.delayed(Duration(seconds: 5));
sunvox.stop();
sunvox.shutDown();
exit(0);
}
代码说明
-
导入必要的包:
import 'dart:async'; import 'dart:io'; import 'package:dart_sunvox/dart_sunvox.dart';
-
初始化SunVox库:
final sunvox = LibSunvox(0); final v = sunvox.versionString(); print('sunvox lib version: $v');
这里创建了一个
LibSunvox
实例并获取了库版本信息。 -
加载音频文件:
const filename = "sunvox_lib/resources/song01.sunvox"; await sunvox.load(filename); // 或者使用Dart的文件操作读取数据 // final data = File(filename).readAsBytesSync();
加载一个
.sunvox
文件到SunVox库。 -
设置音量:
sunvox.volume = 256;
-
开始播放:
sunvox.play(); print("playing:$filename ...");
-
监听播放进度:
int _lastLine = 0; Timer.periodic(Duration(milliseconds: 50), (timer) { final line = sunvox.currentLine; if (_lastLine != line) { _lastLine = line; print("line: $_lastLine"); } });
使用定时器周期性地检查当前播放行数。
-
停止播放:
await Future<void>.delayed(Duration(seconds: 5)); sunvox.stop(); sunvox.shutDown(); exit(0);
更多关于Flutter音频合成与播放插件dart_sunvox的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复