diff --git a/README.md b/README.md index 3d2278b1..362f8539 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,9 @@ The following constants are reported as the only parameter to the - __duration__: The duration of the media, in seconds. +### Class methods + +- `Media.shouldReleaseOnMemoryWarning`: Define if the plugin should automatically release its resources when a memory warnings is received (iOS). ## media.getCurrentAmplitude @@ -670,3 +673,25 @@ function when an error occurs. - `MediaError.MEDIA_ERR_NETWORK` = 2 - `MediaError.MEDIA_ERR_DECODE` = 3 - `MediaError.MEDIA_ERR_NONE_SUPPORTED` = 4 + +## Media.shouldReleaseOnMemoryWarning + +Static method to define if the plugin should automatically stop all sounds and release all possible resources when a memory warning is received. Default value is `true`. + + Media.shouldReleaseOnMemoryWarning(status); + +### Parameters + +- __status__: Boolean value + +### Supported Platforms + + +- iOS + +### Quick Example + +```js +Media.shouldReleaseOnMemoryWarning(false); +// Here comes custom code to handle memoryWarnings +``` \ No newline at end of file diff --git a/src/ios/CDVSound.h b/src/ios/CDVSound.h index 1dca574a..dbdc6168 100644 --- a/src/ios/CDVSound.h +++ b/src/ios/CDVSound.h @@ -91,10 +91,13 @@ typedef NSUInteger CDVMediaMsg; @property (nonatomic, strong) AVAudioSession* avSession; @property (nonatomic, strong) NSString* currMediaId; +- (void)pluginInitialize; + - (void)startPlayingAudio:(CDVInvokedUrlCommand*)command; - (void)pausePlayingAudio:(CDVInvokedUrlCommand*)command; - (void)stopPlayingAudio:(CDVInvokedUrlCommand*)command; - (void)seekToAudio:(CDVInvokedUrlCommand*)command; +- (void)shouldReleaseOnMemoryWarning:(CDVInvokedUrlCommand*)command; - (void)release:(CDVInvokedUrlCommand*)command; - (void)getCurrentPositionAudio:(CDVInvokedUrlCommand*)command; - (void)resumeRecordingAudio:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/CDVSound.m b/src/ios/CDVSound.m index df40d7de..09795a24 100644 --- a/src/ios/CDVSound.m +++ b/src/ios/CDVSound.m @@ -29,6 +29,13 @@ @implementation CDVSound @synthesize soundCache, avSession, currMediaId; +static BOOL releaseOnMemoryWarning; + +- (void)pluginInitialize +{ + releaseOnMemoryWarning = false; +} + // Maps a url for a resource path for recording - (NSURL*)urlForRecording:(NSString*)resourcePath { @@ -565,6 +572,10 @@ - (void)seekToAudio:(CDVInvokedUrlCommand*)command [self.commandDelegate evalJs:jsString]; } +- (void)shouldReleaseOnMemoryWarning:(CDVInvokedUrlCommand*)command +{ + releaseOnMemoryWarning = [[command argumentAtIndex:0] boolValue]; +} - (void)release:(CDVInvokedUrlCommand*)command { @@ -808,9 +819,11 @@ -(void)itemStalledPlaying:(NSNotification *) notification { - (void)onMemoryWarning { - [[self soundCache] removeAllObjects]; - [self setSoundCache:nil]; - [self setAvSession:nil]; + if (releaseOnMemoryWarning) { + [[self soundCache] removeAllObjects]; + [self setSoundCache:nil]; + [self setAvSession:nil]; + } [super onMemoryWarning]; } diff --git a/www/Media.js b/www/Media.js index 5806b9f6..924d9bac 100644 --- a/www/Media.js +++ b/www/Media.js @@ -233,6 +233,19 @@ Media.onStatus = function(id, msgType, value) { }; +/** + * Define if the plugin should release its resources automatically when a memory warning is received (iOS only) + */ +Media.shouldReleaseOnMemoryWarning = function(value) { + if (cordova.platformId === 'ios'){ + exec(null, function () { + console.warn('Unable to set memory warning auto-release flag'); + }, "Media", "shouldReleaseOnMemoryWarning", [value]); + } else { + console.warn('Media.shouldReleaseOnMemoryWarning method is currently not supported for', cordova.platformId, 'platform.'); + } +}; + module.exports = Media; function onMessageFromNative(msg) {