Flutter音频转换插件opus_to_pcm的使用

Flutter音频转换插件opus_to_pcm的使用

opus_to_pcm

插件用于将特定格式的OPUS音频转换为通用的PCM格式。

开始使用

本项目是一个Flutter插件包的起点,它包括适用于Android和/或iOS的平台特定实现代码。

对于帮助你开始Flutter开发,你可以查看在线文档,其中提供了教程、示例、移动开发指南以及完整的API参考。

使用方法

首先,在pubspec.yaml文件中添加opus_to_pcm依赖。

dependencies:
    opus_to_pcm: ^0.0.23

然后,导入库到你的应用中。

import 'package:opus_to_pcm/opus_to_pcm.dart';

接下来,声明对象。

final _opusToPcmPlugin = OpusToPcm();

之后,调用转换方法并传递一个List<int>类型的字节数组参数。

final List<int> _testData = [];
List<int> _result = await _opusToPcmPlugin.opusBytesToPcmBytes(_testData);

最后,你将获得转换后的通用PCM音频数据流。

完整示例

以下是一个完整的示例,展示了如何使用opus_to_pcm插件进行音频转换。

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:opus_to_pcm/opus_to_pcm.dart';
import 'package:opus_to_pcm/sn_filePath_tool.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _opusToPcmPlugin = OpusToPcm();

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    if (!mounted) return;

    setState(() {});
  }

  void sn_testOpusFileConvertPcmFile() async {
    String _filePath = await sn_moveFileToLib();
    Map result = await _opusToPcmPlugin.opusFileToPcmFile(_filePath, "");
    print("sn_log => sn_testOpusFileConvertPcmFile ${result}");
  }

  static sn_moveFileToLib() async {
    String _fileName = 'ota.bin';
    String filePath = 'audio/${_fileName}';
    final ByteData data = await rootBundle.load(filePath);
    final List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    String _filePath = await SNFilePathTool.sn_getFilePath(_fileName);
    File file = File(_filePath);
    await file.writeAsBytes(bytes);
    print("该文件bytes大小为 ${bytes.length}");
    return _filePath;
  }

  void sn_testOpusToPcm() async {
    String _fileName = 'zlftest001.opus';
    String filePath = 'audio/${_fileName}';
    final ByteData data = await rootBundle.load(filePath);
    final List<int> _testDat = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    List<int> _result = await _opusToPcmPlugin.opusBytesToPcmBytes(_testDat);
    Int16List _result_int16 = Int16List.fromList(_result);
    List<int> _result_int8 = _result_int16.buffer.asUint8List(_result_int16.offsetInBytes, _result_int16.lengthInBytes);
    String _testName = await SNFilePathTool.sn_getFilePath("test007.pcm");
    File file = File(_testName);
    await file.writeAsBytes(_result_int8, flush: true);
    print("sn_log =>测试验证pcm存储完成-202405091719" + "\n新的文件路径在${file}");
  }

  void sn_testFirmwareUpgrade() async {
    try {
      String testOTA = await sn_moveFileToLib();
      _opusToPcmPlugin.firmwareUpdate(
          testOTA, "90:B0:3F:57:23:B9", "DELI_MP503W_6789AB", (Map result) {
        int progress = result["progress"];
        int total = result["total"];
        print("!!!! test ==> ${result},\n百分比${(progress / total) * 100}%");
      });
    } catch (e) {
      print("firmwareUpdate e=> ${e}");
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app 2'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}

更多关于Flutter音频转换插件opus_to_pcm的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter音频转换插件opus_to_pcm的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


opus_to_pcm 是一个 Flutter 插件,用于将 Opus 编码的音频文件转换为 PCM 格式。PCM(脉冲编码调制)是一种未压缩的音频格式,常用于音频处理和播放。以下是如何在 Flutter 项目中使用 opus_to_pcm 插件的步骤和示例代码。

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 opus_to_pcm 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  opus_to_pcm: ^1.0.0  # 请使用最新版本

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

2. 导入插件

在 Dart 文件中导入 opus_to_pcm 插件:

import 'package:opus_to_pcm/opus_to_pcm.dart';

3. 使用插件进行音频转换

以下是一个简单的示例,展示如何使用 opus_to_pcm 插件将 Opus 文件转换为 PCM 格式:

import 'package:flutter/material.dart';
import 'package:opus_to_pcm/opus_to_pcm.dart';
import 'dart:io';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: AudioConverterScreen(),
    );
  }
}

class AudioConverterScreen extends StatefulWidget {
  [@override](/user/override)
  _AudioConverterScreenState createState() => _AudioConverterScreenState();
}

class _AudioConverterScreenState extends State<AudioConverterScreen> {
  String _outputPath = '';

  Future<void> convertOpusToPcm() async {
    // Opus 文件路径
    String opusFilePath = 'path/to/your/opus/file.opus';

    // 输出 PCM 文件路径
    String pcmFilePath = '${Directory.systemTemp.path}/output.pcm';

    try {
      // 使用插件进行转换
      await OpusToPcm.convert(opusFilePath, pcmFilePath);

      setState(() {
        _outputPath = pcmFilePath;
      });

      print('转换成功,PCM 文件保存到: $_outputPath');
    } catch (e) {
      print('转换失败: $e');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Opus to PCM 转换'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: convertOpusToPcm,
              child: Text('转换 Opus 到 PCM'),
            ),
            SizedBox(height: 20),
            Text('PCM 文件路径: $_outputPath'),
          ],
        ),
      ),
    );
  }
}
回到顶部