AudioPlayers 无法在 iOS 上运行 | 颤振插件

Mar*_*rio 2 audio file ios flutter flutter-audioplayers

我正在创建一个 Flutter 插件。目前,代码:

  1. 将字符串合成为音频文件
  2. 获取文件的路径
  3. 使用音频播放器播放音频文件

当我在 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';
  }
Run Code Online (Sandbox Code Playgroud)

我实现了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> 
Run Code Online (Sandbox Code Playgroud)

使用AudioCache问题

有人还有其他建议吗?我是否需要将文件移至本地资产并用于AudioCache重现该文件?

更新

我也尝试添加标志isLocal

  playLocal() async {
    int result = await audioPlayer.play(localPath, isLocal: true);
  }
Run Code Online (Sandbox Code Playgroud)

根据文档是需要原因

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
Run Code Online (Sandbox Code Playgroud)

我也测试过playLocal()使用错误的路径进行调用。它不断返回1,但之后显示以下错误:

FigFileForkOpenMainByCFURL 发出 err=2 (errno) (打开失败)信号

尽管如此,使用正确的方法调用方法不会触发错误localPath,所以我认为它应该正确播放。

笔记

如果我不打电话,而是playLocal()打电话flutter_tts.speak('test'),那就有效了。因此,该错误可能与 相关audioplayers

我正在使用的版本是flutter_tts ^2.1.0audioplayers: ^0.17.3

小智 8

final AudioContext audioContext = AudioContext(
    iOS: AudioContextIOS(
      defaultToSpeaker: true,
      category: AVAudioSessionCategory.ambient,
      options: [
        AVAudioSessionOptions.defaultToSpeaker,
        AVAudioSessionOptions.mixWithOthers,
      ],
    ),
    android: AudioContextAndroid(
      isSpeakerphoneOn: true,
      stayAwake: true,
      contentType: AndroidContentType.sonification,
      usageType: AndroidUsageType.assistanceSonification,
      audioFocus: AndroidAudioFocus.none,
    ),
  );
  AudioPlayer.global.setGlobalAudioContext(audioContext);
Run Code Online (Sandbox Code Playgroud)

将其添加到 main.dart 文件中的 runApp(App()); 之前