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);
}

代码说明

  1. 导入必要的包

    import 'dart:async';
    import 'dart:io';
    import 'package:dart_sunvox/dart_sunvox.dart';
    
  2. 初始化SunVox库

    final sunvox = LibSunvox(0);
    final v = sunvox.versionString();
    print('sunvox lib version: $v');
    

    这里创建了一个LibSunvox实例并获取了库版本信息。

  3. 加载音频文件

    const filename = "sunvox_lib/resources/song01.sunvox";
    await sunvox.load(filename);
    // 或者使用Dart的文件操作读取数据
    // final data = File(filename).readAsBytesSync();
    

    加载一个.sunvox文件到SunVox库。

  4. 设置音量

    sunvox.volume = 256;
    
  5. 开始播放

    sunvox.play();
    print("playing:$filename ...");
    
  6. 监听播放进度

    int _lastLine = 0;
    
    Timer.periodic(Duration(milliseconds: 50), (timer) {
      final line = sunvox.currentLine;
      if (_lastLine != line) {
        _lastLine = line;
        print("line: $_lastLine");
      }
    });
    

    使用定时器周期性地检查当前播放行数。

  7. 停止播放

    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 回复

更多关于Flutter音频合成与播放插件dart_sunvox的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


dart_sunvox 是一个用于在 Flutter 应用中集成 SunVox 音频合成器的插件。SunVox 是一个强大的模块化音频合成器,支持实时音频合成和播放。通过 dart_sunvox,你可以在 Flutter 应用中利用 SunVox 的功能来合成和播放音频。

安装 dart_sunvox

首先,你需要在 pubspec.yaml 文件中添加 dart_sunvox 依赖:

dependencies:
  dart_sunvox: ^0.1.0

然后运行 flutter pub get 来安装依赖。

基本用法

以下是一个简单的示例,展示如何使用 dart_sunvox 来初始化和播放 SunVox 项目。

import 'package:flutter/material.dart';
import 'package:dart_sunvox/dart_sunvox.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 SunVox
  await SunVox.init();
  
  // 加载 SunVox 项目
  final projectPath = 'assets/my_project.sunvox';
  final projectHandle = await SunVox.loadProject(projectPath);
  
  // 播放项目
  SunVox.play(projectHandle);
  
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('SunVox Player'),
        ),
        body: Center(
          child: Text('Playing SunVox project...'),
        ),
      ),
    );
  }
}
回到顶部