opus_to_pcm 0.0.19 copy "opus_to_pcm: ^0.0.19" to clipboard
opus_to_pcm: ^0.0.19 copied to clipboard

Plugin for common pcm for audio conversion in a specific opus format.

example/lib/main.dart

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
  State<MyApp> createState() => _MyAppState();
}

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

  // //接收安卓端向flutter传递的数据
  // MethodChannel _progressChannel = MethodChannel('opus_to_pcm');

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.

    // 测试转换
    // sn_testOpusToPcm();
    // sn_testOpusFileConvertPcmFile();
    // 固件升级
    sn_testFirmwareUpgrade();

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {});
  }

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

  /// 调试用
  static sn_moveFileToLib() async {
    // String filePath = 'audio/zlftest001.opus';
    // String _fileName = 'zlftest001.opus';
    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);
    // print("sn_log => _result ${_result}");

    // 转为short
    Int16List _result_int16 = Int16List.fromList(_result);

    // short重新转为List<int>
    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 {
    // _progressChannel.setMethodCallHandler((MethodCall call) async {
    //   switch (call.method) {
    //     case 'progress':
    //       print("flutter progress =>  ${call.arguments}");
    //       break;
    //   }
    // });

    if (Platform.isAndroid) {
      try {
        bool isPairDevice =
            await _opusToPcmPlugin.isPairDevice("90:B0:87:38:B1:E7");
        if (isPairDevice) {
          String testOTA = await sn_moveFileToLib();
          _opusToPcmPlugin.firmwareUpdate(
              testOTA, "90:B0:87:38:B1:E7", "DELI_MP503W_6789AB", (Map result) {
            print("!!!! test ==>>> ${result}");
          });
        } else {
          await _opusToPcmPlugin.pairDevice("90:B0:87:38:B1:E7");
        }
      } catch (e) {
        print("firmwareUpdate e=> ${e}");
      }
    } else {
      _opusToPcmPlugin.firmwareUpdate(
          "testOTA_filePath", "90:B0:87:38:B1:E7", "DELI_MP503W_6789AB",
          (Map result) {
        print("!!!! cb test ==>>> ${result}");
      });
    }
  }

  @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'),
        ),
      ),
    );
  }
}
1
likes
0
points
10
downloads

Publisher

unverified uploader

Weekly Downloads

Plugin for common pcm for audio conversion in a specific opus format.

License

unknown (license)

Dependencies

device_info_plus, flutter, path_provider, permission_handler, plugin_platform_interface

More

Packages that depend on opus_to_pcm

Packages that implement opus_to_pcm