Search code examples
iosmedia-playercodenameone

Video and audio playback not working on iOS


I'm trying to incorporate video and audio file attachment to forum posts in an app, and the playback isn't working on iOS. On Android it works fine. The code looks like this (for audio, and similar for video):

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();
    }
});

I don't see any errors in the console, and when debugging I see the catch block is not entered. However, no sound comes out of the device when I try to play the audio, and the video player remains blank, giving me this message in the console:

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))

What am I missing?


Solution

  • The problem was not at all media playback. The file wasn't actually present on the device, even though no exception was reported by iOS nor was the catch clause ever entered. My incorrect assumptions were 1. that I could use try/catch around the input stream instantiation as a means of checking if the file exists (it worked on Android but not iOS), and 2. that the operating system would necessarily throw an exception if the file wasn't present (not the same as 1., but tied into it).

    My new, working code looks like this:

    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();