Phonegap "['Media'] The plugin should use a background thread."

I am using Phonegap 3 and the Media plugin. I keep getting these errors when testing my application on iOS:

THREAD WARNING: ['Media'] took '205.391846' ms. Plugin should use a background thread. 

I saw this from the documentation on phone records ( http://docs.phonegap.com/en/edge/guide_platforms_ios_plugin.md.html ):

  - (void)myPluginMethod:(CDVInvokedUrlCommand*)command { // Check command.arguments here. [self.commandDelegate runInBackground:^{ NSString* payload = nil; // Some blocking logic... CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:payload]; // The sendPluginResult method is thread-safe. [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; } 

Is this included in my application, or do I need to edit it for each plugin? Where can I put it?

I saw several posts on the Internet about this, but none of them gave a clear answer to the question of how to use the above code.

+6
source share
2 answers

Personally, I haven't used the Media plugin yet, but to handle background streams you will need to check which call warning calls.

For example, if this warning occurs when creating a Media object:

 var my_media = new Media(src, onSuccess, onError); 

Then you can check out the .js plugin file (which is Media.js ). Find the Media function and find your own call, which in this case:

 exec(null, this.errorCallback, "Media", "create", [this.id, this.src]); 

From this, you know that it calls the Media class create method.
So go and open Media.m (or CDVSound.m in this exact case) and find the create method (you should find it in cordova/plugins/org.apache.cordova.media/src/ios ), complete the encapsulation of the whole method with help

 [self.commandDelegate runInBackground:^{ // the create method goes here }]; 

This will create a background stream for the 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]; } }]; } 
+10
source

1. Open the xCode project.

2. Click on the settings tab 2 “Features”.

3. Turn on “Background Modes” on.

4. Select "Audio and Broadcast."

Done. Now you can play music in the background.

PS: I am using xCode 6 and Cordova 3.6.3

-4
source

Source: https://habr.com/ru/post/971759/


All Articles