【发布时间】:2021-02-02 08:46:35
【问题描述】:
我正在创建一个 Flutter 插件。目前,代码:
- 将字符串合成为音频文件
- 获取文件路径
- 使用audioplayers 播放音频文件
当我在 Android 中运行该应用程序时,它可以完美运行。然而,当我在 iOS 上运行它时,它不会(假设相关权限/限制)
await flutterTts
.synthesizeToFile(
"This is my first audio synthesizer in Flutter", audioFileName)
.then((success) => {
if (success == 1)
{
print('Successfully synthesized!!'),
// Gets the path to the generated file depending on the platform
pathFile =
Platform.isAndroid ? pathToFileAndroid : pathToFileIos,
pathFile
.then((pathToAudioFile) => {
print('Path to file: ' + pathToAudioFile),
// Speakers/earpiece
// int result = await player.earpieceOrSpeakersToggle();
// Sets the path to the file
audioPlayer.setUrl(pathToAudioFile),
audioPlayer.setVolume(1.0),
// Gets the duration of the audio file to be reproduced
// Plays the audio file
playLocal(audioPlayer, pathToAudioFile),
}) // pathFile
.catchError((e) {
print('Error: Unable to get the path to the audio file');
}),
}, // success
}) //synthesizeToFile
.catchError((e) {
print('Error during the synthesisation: $e');
});
}
/// Gets the path to the file to be accessed (iOS)
static Future<String> get pathToFileIos async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
print('appDocPath: $appDocPath');
return '$appDocPath/$audioFileName';
}
我实现了audioPlayer.onDurationChanged.listen 来确认文件的路径是正确的(它实际上得到了文件的持续时间)。然而,该文件并未播放。
我正在使用模拟器测试应用程序,这是存放文件的路径
/localPaty/Devices/deviceID/data/Containers/Data/Application/appID/Documents/temp_audio_cue.caf
我阅读了有关此主题的不同问题,但没有找到适合我的案例的解决方案
我试过了:
按照文档的建议编辑传输安全
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
使用AudioCache (question)
有人有其他建议吗?我需要将文件移动到我的本地资产并使用AudioCache 来重现文件吗?
更新
我也尝试添加标志isLocal。
playLocal() async {
int result = await audioPlayer.play(localPath, isLocal: true);
}
根据documentation是需要的原因
isLocal 标志是必需的,因为 iOS 和 macOS 对此有所不同
它返回1,所以我猜这意味着成功,但声音仍然没有触发。可能是模拟器失败了?
"iOS => call startHeadlessService, playerId ID"
"iOS => call setUrl, playerId ID"
"iOS => call setVolume, playerId ID"
"iOS => call play, playerId ID"
2021-02-03 11:59:33.149925+0100 Runner[61496:850044] flutter: Played successfully?: 1
我还测试了以错误的路径调用playLocal()。它不断返回1,但之后显示以下错误:
FigFileForkOpenMainByCFURL 发出信号 err=2 (errno)(打开失败)
尽管如此,使用正确的localPath 调用方法不会触发错误,所以我认为它应该可以正确播放。
注意
如果我不打电话给playLocal(),而是打电话给flutter_tts.speak('test'),它会起作用。因此,错误应该与audioplayers有关。
我使用的版本是flutter_tts ^2.1.0 和audioplayers: ^0.17.3
【问题讨论】:
-
您好,我有一个问题,我必须在 Flutter 应用程序中添加这些内容。
NSAppTransportSecurity NSAllowsArbitraryLoads