You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lxm 388fa342b0 fix(android): 移除MANAGE_EXTERNAL_STORAGE权限声明 1 year ago
..
android fix(android): 移除MANAGE_EXTERNAL_STORAGE权限声明 1 year ago
lib Merge branch 'new_dev' of https://github.com/deepcloud2048/deep_voice into new_dev 1 year ago
README.md Merge branch 'new_dev' of https://github.com/deepcloud2048/deep_voice into new_dev 1 year ago
pubspec.yaml Merge branch 'new_dev' of https://github.com/deepcloud2048/deep_voice into new_dev 1 year ago

README.md

JL OPUS Plugin

基于杰理音频编解码库的Flutter插件,提供OPUS音频格式的解码功能。

功能

  • 支持OPUS文件解码为PCM格式
  • 支持OPUS数据流解码
  • 支持多种采样率和通道配置
  • 支持带协议头/不带协议头的OPUS数据

引入方式

在pubspec.yaml中添加依赖:

dependencies:
  jl_opus:
    path: ./local_plugins/jl_opus

使用方法

初始化插件

import 'package:jl_opus/jl_opus.dart';

final jlOpus = JlOpus();

解码OPUS文件

Future<void> decodeOpusFile() async {
  final inPath = '/path/to/input.opus';
  final outPath = '/path/to/output.pcm';
  
  // 创建解码选项
  final option = OpusOption(
    hasHeader: false,    // 是否有协议头
    channel: 1,          // 通道数:1=单声道,2=立体声
    sampleRate: 16000,   // 采样率
    packetSize: 40,      // 数据包大小(仅当hasHeader=false时生效)
  );
  
  try {
    // 开始解码文件
    final outFilePath = await jlOpus.decodeOpusFile(inPath, outPath, option);
    print('解码成功: $outFilePath');
  } catch (e) {
    print('解码失败: $e');
  }
}

解码OPUS数据流

Future<void> decodeOpusStream() async {
  // 监听解码后的PCM数据
  jlOpus.decodedDataStream.listen((pcmData) {
    print('收到解码后的PCM数据: ${pcmData.length} 字节');
    // 处理解码后的PCM数据
  });
  
  // 创建解码选项
  final option = OpusOption(
    hasHeader: false,
    channel: 1,
    sampleRate: 16000,
    packetSize: 40,
  );
  
  // 开始解码数据流
  final success = await jlOpus.startDecodeStream(option);
  if (success) {
    print('开始解码数据流');
    
    // 写入OPUS数据进行解码
    final opusData = Uint8List.fromList([/* OPUS数据 */]);
    await jlOpus.writeAudioStream(opusData);
    
    // 数据处理完成后停止解码
    await jlOpus.stopDecodeStream();
  }
}

监听事件

void listenToEvents() {
  jlOpus.eventStream.listen((event) {
    switch (event.event) {
      case 'onStart':
        print('开始${event.type == "file" ? "文件" : "流"}解码');
        break;
      case 'onComplete':
        print('完成${event.type == "file" ? "文件" : "流"}解码');
        if (event.filePath != null) {
          print('输出文件: ${event.filePath}');
        }
        break;
      case 'onError':
        print('解码错误: [${event.errorCode}] ${event.errorMessage}');
        break;
    }
  });
}

资源释放

Future<void> disposePlugin() async {
  await jlOpus.dispose();
  print('插件资源已释放');
}

注意事项

  1. OPUS解码需要正确设置参数,特别是采样率和通道数必须与原始音频匹配
  2. 使用前请确保jl_audio_decode_V2.0.0_20005.aar包已添加到libs目录
  3. 本插件仅支持Android平台