iOS 无法播放视频和音频

Video and audio playback not working on iOS

我正在尝试将视频和音频文件附件合并到应用中的论坛帖子中,但无法在 iOS 上播放。在 Android 上它工作正常。代码如下所示(对于音频,对于视频类似):

String localAudiofilePath = FileSystemStorage.getInstance().getAppHomePath() + (String) fileInfo.get("path");
InputStream is = null;
Media m = null;
try{
    is = FileSystemStorage.getInstance().openInputStream(localAudiofilePath);
    m = MediaManager.createMedia(is, "audio/" + extractFileExtension((String) fileInfo.get("path")));
}catch(Exception ex){
    ex.printStackTrace();
}

m.play();

final Media mm = m;

Display.getInstance().scheduleBackgroundTask(new Runnable() {
    @Override
    public void run() {
      while(mm.isPlaying()){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {         
        }
      }
      mm.cleanup();
    }
});

我在控制台中没有看到任何错误,在调试时我看到 catch 块没有输入。但是,当我尝试播放音频时设备没有声音,视频播放器仍然空白,在控制台中显示以下消息:

2018-01-18 04:22:20.213822 MyApplication[24425:2262915] [Playback] ❗️Playback failed with error: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x61000024c2d0 {Error Domain=NSOSStatusErrorDomain Code=-12893 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12893), NSLocalizedDescription=The operation could not be completed}, not resolving (canResolve: YES, errorResolver: (null))

我错过了什么?

您正在使用录音 mime 类型播放视频。我想 Android 会忽略它,这很好。 iOS 端口看到流数据的音频格式并假定这是一个音频文件。您需要指定正在播放的视频的 mime 文件类型。

问题根本不是媒体播放。该文件实际上并不存在于设备上,即使 iOS 没有报告异常,也没有输入 catch 子句。我不正确的假设是 1. 我可以在输入流实例化周围使用 try/catch 作为检查文件是否存在的方法(它适用于 Android 但不适用于 iOS),以及 2.如果文件不存在,操作系统必然会抛出异常(与 1. 不同,但与它相关)。

我新的工作代码如下所示:

String localAudiofilePath = FileSystemStorage.getInstance().getAppHomePath() + MyApplication.DIRECTORY_APP_DOWNLOADS + "/" + (String) fileInfo.get("path");
if(!FileSystemStorage.getInstance().exists(localAudiofilePath)){
    Cn1FileUtils.downloadRemoteFile("https://medonline.co.il/uploads/" + (String) fileInfo.get("path"), (String) fileInfo.get("path"), true);
}
InputStream is = null;
media = null;
try{
    is = FileSystemStorage.getInstance().openInputStream(localAudiofilePath);
    media = MediaManager.createMedia(is, "audio/" + Cn1FileUtils.extractFileExtension((String) fileInfo.get("path")));
}catch(Exception ex){
    ex.printStackTrace();        
 ToastBar.showErrorMessage(MyApplication.getInstance().getString("error_loading_file"));
    return;
}
media.play();