Phonegap "['Media'] Plugin should use a background thread."
Personally I haven't yet used the Media
plugin but to handle background threads you will need to check which call is causing the warning
.
For example if this warning throws when you are creating a media
object:
var my_media = new Media(src, onSuccess, onError);
Then you can check the plugins .js
file (which is Media.js
). Look for the function Media
and look for the native call which is in this case:
exec(null, this.errorCallback, "Media", "create", [this.id, this.src]);
From this you know that this is calling the Media
class's create
method.
So go and open Media.m
(or CDVSound.m
in this exact case) and look for the create
method (you should find it in cordova/plugins/org.apache.cordova.media/src/ios
), end encapsulate the whole method with:
[self.commandDelegate runInBackground:^{ // the create method goes here}];
This will create a background thread for "native" Media creation.It should look like this:
- (void)create:(CDVInvokedUrlCommand*)command{ [self.commandDelegate runInBackground:^{ NSString* mediaId = [command.arguments objectAtIndex:0]; NSString* resourcePath = [command.arguments objectAtIndex:1]; CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId:mediaId doValidation:NO forRecording:NO]; if (audioFile == nil) { NSString* errorMessage = [NSString stringWithFormat:@"Failed to initialize Media file with path %@", resourcePath]; NSString* jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMessage]]; [self.commandDelegate evalJs:jsString]; } else { CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; } }];}
1.Open your xCode project.
2.Click the setting tab 2 "Capabilities".
3.Turn “Background Modes” on.
4.Chose "Audio and Airplay".
Done.Now you can play music in background.
PS:I use xCode 6 and Cordova 3.6.3