我正在使用just_audio包播放我的base64字符串,这是我从服务器收到的wav文件。该应用程序正在android设备API 30-ish上播放。这是base64加载后的错误:
E/flutter (31770): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method disposeAllPlayers on channel com.ryanheise.just_audio.methods)
E/flutter (31770): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #1 MethodChannelJustAudio.disposeAllPlayers (package:just_audio_platform_interface/method_channel_just_audio.dart:29:10)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770):
E/flutter (31770): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method init on channel com.ryanheise.just_audio.methods)
E/flutter (31770): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #1 MethodChannelJustAudio.init (package:just_audio_platform_interface/method_channel_just_audio.dart:13:5)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #2 AudioPlayer._setPlatformActive.setPlatform (package:just_audio/just_audio.dart:1330:13)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770):
我的自定义base64播放器,我遵循页面的说明:
class MyAudioPlayer extends StreamAudioSource {
MyAudioPlayer({this.bytes});
List<int> bytes;
@override
Future<StreamAudioResponse> request([int start, int end]) async {
final start = 0;
final end = bytes.length;
return StreamAudioResponse(
sourceLength: bytes.length,
contentLength: end - start,
offset: start,
stream: Stream.value(bytes.sublist(start, end)),
contentType: "audio/x-wav",
);
}
}
我播放的文件:
void playTTS(String content) async{
Dialogs.showLoadingDialog(context: context);
final List<int> base64 = await ApiService.textToSpeech(content);
final player = AudioPlayer();
await player.setAudioSource(MyAudioPlayer(bytes: base64));
await player.play();
Navigator.of(context, rootNavigator: true).pop();
listString.value = List.from(listString.value..add(ValueNotifier(content)));
}
我已经将我的androidmanifest.xml设置为true。
android:usesCleartextTraffic="true"
感谢您的帮助。
我似乎不必像教程中那样用自定义类转换base64 Wav文件。根据在线转换器,我所要做的就是把data:audio/wav;base64,$base64
放在实际的base64字符串之前,并使用正常的setUrl
,它起作用了:
final String code = "data:audio/wav;base64," + json.decode(response.body)["base64"];
final player = AudioPlayer();
player.setUrl(code);
await player.play();