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.
|
- import 'package:audioplayers/audioplayers.dart';
- import 'dart:typed_data';
- import 'dart:async';
- import 'dart:io';
-
- import 'package:demo001/plugin/xunfei/audiotranslate/result_audio.dart';
-
- class AudioPlayerHandler {
- final AudioPlayer _audioPlayer = AudioPlayer();
- final Xunfei_AudioTranslation_Result_Audio _audioStream;
-
- AudioPlayerHandler(this._audioStream);
-
- // 播放音频流
- Future<void> playAudio() async {
- // 从音频流获取数据并播放
- await for (Uint8List audioData in _audioStream.audioStream) {
- await _playAudioData(audioData);
- }
- }
-
- // 播放音频数据
- Future<void> _playAudioData(Uint8List audioData) async {
- // 暂时将音频数据保存到文件系统
- final file = await _saveToFile(audioData);
- // 播放文件
- await _audioPlayer.play(DeviceFileSource(file.path));
- }
-
- // 保存音频数据到文件
- Future<File> _saveToFile(Uint8List audioData) async {
- final directory = await Directory.systemTemp.createTemp();
- final file = File('${directory.path}/audio.pcm');
- await file.writeAsBytes(audioData);
- return file;
- }
- }
|