Flutter iOS 存储音频无法使用音频播放器、fluttersound 和仅音频播放

发布于 2025-01-20 11:02:12 字数 1560 浏览 2 评论 0原文

我正在开发一个可以录制语音并从存储中播放的应用程序。我已经尝试了所有这些软件包,但仅在真实的 iOS 设备上出现错误。在模拟器方面,ios 上也没有问题。我正在使用 flutter flutter_sound 录制音频。

目录正在

tempDir = await getTemporaryDirectory();

录制;

  startRecorderx(
  FlutterSoundRecorder flutterSoundRecorder, Directory? tempDir) async {
log(tempDir!.path.toString());
PermissionStatus status;
try {
  status = await Permission.microphone.request();
} catch (e) {
  throw e;
}
log(tempDir.path.toString());

if (status != PermissionStatus.granted)
  throw RecordingPermissionException("You must give acces to mic");
pathToRecord =
    "${tempDir.path}/${DateTime.now().millisecondsSinceEpoch.toString()}.aac";

await flutterSoundRecorder.startRecorder(
  toFile: "$pathToRecord",
  codec: Codec.aacADTS,
);

然后

我无法从路径 netiher 3 packages audioplayers flutter_sound 播放此文件。

     play(path) async {
    File file = File(path);
    Uint8List bytes =  file.readAsBytesSync();
     await audio.play(path);
    //log(result.toString());
    /* await flutterSoundPlayer.startPlayer(
       //fromURI: "$path",
       fromDataBuffer: bytes,
     ) ;*/
    update();
  }

我仅从 flutter_sound 收到错误。

PlatformException (PlatformException(Audio Player, startPlayer failure, null, null))

I am working on an app that can record voice and play from storage. I have tried all these packages and get errors only on real iOS devices. On emulator side there is no issue on ios too. I am recording audio with flutter flutter_sound.

directory is

tempDir = await getTemporaryDirectory();

recording;

  startRecorderx(
  FlutterSoundRecorder flutterSoundRecorder, Directory? tempDir) async {
log(tempDir!.path.toString());
PermissionStatus status;
try {
  status = await Permission.microphone.request();
} catch (e) {
  throw e;
}
log(tempDir.path.toString());

if (status != PermissionStatus.granted)
  throw RecordingPermissionException("You must give acces to mic");
pathToRecord =
    "${tempDir.path}/${DateTime.now().millisecondsSinceEpoch.toString()}.aac";

await flutterSoundRecorder.startRecorder(
  toFile: "$pathToRecord",
  codec: Codec.aacADTS,
);

}

Then i can not play this file from path netiher 3 packages audioplayers, flutter_sound.

     play(path) async {
    File file = File(path);
    Uint8List bytes =  file.readAsBytesSync();
     await audio.play(path);
    //log(result.toString());
    /* await flutterSoundPlayer.startPlayer(
       //fromURI: "$path",
       fromDataBuffer: bytes,
     ) ;*/
    update();
  }

I got error from flutter_sound only.

PlatformException (PlatformException(Audio Player, startPlayer failure, null, null))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2025-01-27 11:02:12

路径的问题。 iOS返回总是不同的路径。我想他们掩盖了目录目录的“安全”路径。如果您要存储文件的完整路径,则可以通过该功能获得真正的路径,这是我的情况,并且串起您的路径取决于您的情况,请注意您的情况;

applicationDocumentDirectory在哪里保存文件;
/用户/alperenbaskaya/library/

developer/coreSimulator/devices/d3c845c6-d8ce-40fa-9060-1d8062e7f597/data/data/data/data/data/application/application/application/application/17501EFE-57A4-4C71-4C71-8C4C4C-58BBC5117DOUMTION 934.jpg,我的操作是那;

  Future<String> getCurrentUrl(String url)async{
    if(Platform.isIOS){
    String a = url.substring(url.indexOf("Documents/") + 10, url.length) ;
    Directory dir = await getApplicationDocumentsDirectory();
    a = "${dir.path}/$a";
    return a;
    }
    else{
      return url;
    }
  }

The problem with path. iOS returns always different paths. They masks directory's path for 'security' I guess. If you are storing full path of a file you can get real path with the function, it is my case and substringing your path depends on your case, please mind your case;

where applicationDocumentDirectory saves file ;
/Users/alperenbaskaya/Library/Developer/CoreSimulator/Devices/D3C845C6-D8CE-40FA-9060-1D8062E7F597/data/Containers/Data/Application/17501EFE-57A4-4C71-8C4C-58BBC57D1177/Documents/1661040154934.jpg

then my operation is that;

  Future<String> getCurrentUrl(String url)async{
    if(Platform.isIOS){
    String a = url.substring(url.indexOf("Documents/") + 10, url.length) ;
    Directory dir = await getApplicationDocumentsDirectory();
    a = "${dir.path}/$a";
    return a;
    }
    else{
      return url;
    }
  }
等风也等你 2025-01-27 11:02:12

对于iOS,只需添加文件://目录路径目录

dir dir =等待getApplicationDocumentsDirectory();

字符串fullurl =“ file:// $ {dir.path}/mp3-file-name”;

For iOS just add file:// before directory path

Directory dir = await getApplicationDocumentsDirectory();

String fullUrl = "file://${dir.path}/MP3-FILE-NAME";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文