gpt4 book ai didi

multithreading - Phonegap "[' Media'] 插件应该使用后台线程。”

转载 作者:行者123 更新时间:2023-12-04 14:44:43 25 4
gpt4 key购买 nike

我正在使用 Phonegap 3 和媒体插件。在 iOS 上测试我的应用程序时,我不断收到这些错误:

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

我从 phonegap 文档( 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];
}];
}

这会按原样进入我的应用程序,还是我需要为每个插件编辑它?我把它放在哪里?

我在网上看到了一些关于这个的帖子,但没有一个明确回答如何使用上述代码。

最佳答案

我个人还没有用过Media插件但要处理后台线程,您需要检查哪个调用导致 warning .

例如,如果在创建 media 时抛出此警告目的:

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

然后你可以查看插件 .js文件(即 Media.js )。寻找函数 Media并查找本例中的 native 调用:
exec(null, this.errorCallback, "Media", "create", [this.id, this.src]);

由此你知道这是在调用 Media类(class) create方法。
所以去打开 Media.m (或 CDVSound.m 在这种情况下)并寻找 create方法(您应该在 cordova/plugins/org.apache.cordova.media/src/ios 中找到它),用以下代码结束整个方法的封装:
[self.commandDelegate runInBackground:^{
// the create method goes here
}];

这将为“原生”媒体创建创建一个后台线程。
它应该是这样的:
- (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];
}
}];
}

关于multithreading - Phonegap "[' Media'] 插件应该使用后台线程。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24572174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com