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