Does background playback still work with cordova media plugin on iOS 10?

1.5k views Asked by At

I'm trying to get an audio sample to play in an Ionic/Cordova App when a bluetooth device goes out of range and the iOS device is either in standby (screen locked) or the App is minimised to the background.

I'm using a similar function to the example in the Cordova media plugin documentation, with a function to correct the audio file path for Android devices:

function getMediaURL(url) {
    if(device.platform.toLowerCase() === "android") return "/android_asset/www/" + url;
    return url;
};

function soundAlarm() {
    var mediaUrl = getMediaURL('sounds/alarm.wav');

    alarmSound = new Media( mediaUrl, null,
    // error callback
    function (err) {
        console.log("playAudio():Audio Error: " + angular.toJson(err));
    });

    alarmSound.play({ playAudioWhenScreenIsLocked : true, numberOfLoops: 3 });
}

This works fine when the App is in the foreground however when I lock the phone or minimise the App I get the following error:

{"message":"","code":4}

According to the Cordova media plugin documentation, error code 4 means

MEDIA_ERR_NONE_SUPPORTED

The 'playAudioWhenScreenIsLocked' option I pass into the play method is supposed to handle the issue I'm having however I just get the above error every time. Besides, this property is set to true by default.

I don't want to have to use a background mode plugin as this functionality should work out of the box via cordova media plugin and the background mode plugin may cause the App to be rejected from the App Store.

Has anyone had any success triggering audio while the App is in the background or the device is in standby mode?

1

There are 1 answers

2
Sunny Patel On BEST ANSWER

I had to enable 'Background Modes' and check 'Audio, Airplay, and Picture in Picture' from Xcode to get it to work.