Search code examples
ioscordovamediaphonegap-buildcordova-media-plugin

Cordova Media Plugin not playing local file on iOS


I'm having problem on playing downloaded audio file from local storage using Media plugin.

I'm downloading the file using FileTransfer plugin to LocalFileSystem.TEMPORARY directory.

Before playing, I check the existence of audio file using following code:

window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) {
        var rootdir = fileSystem.root;
        var dir_path = rootdir.toURL();
        
        if (success_callback) {
            if (file_name && file_name !== "") {
                success_callback(dir_path + file_name);
            } else {
                success_callback(dir_path);
            }
        }
    }, function () {
        if (error_callback) {
            error_callback("Error in FileSystem request");
        }
    });

The above method verifies that the file is present in Temporary storage directory.

But when I play the file, I the error with following logs on iOS:

> File Transfer Finished with response code 200
> Audio downloaded successfully. Location: file:///var/mobile/Containers/Data/Application/0B5359AD-0F9E-4FB1-BEB3-71713973A8A2/tmp/user_timestamp.wav
> Play local file from: file:///var/mobile/Containers/Data/Application/0B5359AD-0F9E-4FB1-BEB3-71713973A8A2/tmp/user_timestamp.wav
> Will attempt to use file resource from LocalFileSystem.TEMPORARY directory
> Failed to initialize AVAudioPlayer: The operation couldn’t be completed. (OSStatus error 2003334207.)
> playAudio() Error: {"message":"","code":4}

I've tried providing full path and just the file name to Media object but both approaches don't work. Any help will be greatly appreciated.


Solution

  • I found out that the problem. The thing is iOS is not playing files recorded on Android. It plays the iOS recorded files without any problem.

    The following link helped me set the Media Plugin files AudioPlayer.java in Android and CDVSound.m in iOS to similar settings and now the audios of each platform is playing on other.

    audio format for iOS and Android