【问题标题】:Change the cordova-nativeaudio plugin更改 cordova-nativeaudio 插件
【发布时间】:2017-04-09 10:35:48
【问题描述】:

我正在尝试更改 Cordova 插件,以便可以从 javascript 更改 iOS 音乐类别,但我在 Cordova 构建日志中收到警告

知道我的代码有什么问题吗?第一个参数有效,第二个无效

audio.setCategory('AVAudioSessionCategoryAmbient', 
                  'AVAudioSessionCategoryOptionMixWithOthers')

和 iOS 部分:

- (void) setCategory:(CDVInvokedUrlCommand *)command {

    NSArray* arguments = command.arguments;
    NSString *category = [arguments objectAtIndex:0];
    NSString *options = [arguments objectAtIndex:1];

    [[AVAudioSession sharedInstance] setCategory:category withOptions:options error:nil];


    [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}

我收到以下警告

/project/koeriersapp/Plugins/cordova-plugin-nativeaudio/NativeAudio.m:67:71: 警告:指向整数转换的不兼容指针发送'NSString *__strong' 到“AVAudioSessionCategoryOptions”类型的参数(又名“枚举 AVAudioSessionCategoryOptions”)[-Wint-conversion] [[AVAudioSession sharedInstance] setCategory:category withOptions:options error:nil]; ^~~~~~~~~~~~ 在从导入的模块'AVFoundation'中 /project/koeriersapp/Plugins/cordova-plugin-nativeaudio/NativeAudio.h:11: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.1.sdk/System/Library/Frameworks/AVFoundation.framework/Frameworks/AVFAudio.framework/Headers/AVAudioSession.h:364 :85: 注意:在此处将参数传递给参数“选项” - (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError NS_AVAILABLE_IOS(6_0); ^

【问题讨论】:

  • 仅供参考,phongap 已弃用,请使用 cordova 名称

标签: javascript ios cordova


【解决方案1】:

您不能将NSString 作为options 传递给:

[[AVAudioSession sharedInstance] setCategory:category withOptions:options error:nil]; 

应该是AVAudioSessionCategoryOptions枚举。

要解决它,您可以将匹配器编写为:

AVAudioSessionCategoryOptions optionsOrig = 0;

if([options isEqualToString: @"AVAudioSessionCategoryOptionMixWithOthers"]){
    optionsOrig = AVAudioSessionCategoryOptionMixWithOthers;
}

[[AVAudioSession sharedInstance] setCategory: category withOptions:optionsOrig error:nil];

参考:

- (BOOL)setCategory:(NSString *)category 
    withOptions:(AVAudioSessionCategoryOptions)options 
          error:(NSError * _Nullable *)outError;

参见文档here

【讨论】:

  • 所以应该是0x1和0x2这样的数字?
  • 为什么这有效然后 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil];
  • @JeroenWelzenVan 因为AVAudioSessionCategoryOptionDuckOthers 不是字符串而是数字
  • 我不明白如何解决这个问题,你能给我一个例子吗
  • @JeroenWelzenVan 好吧,我在回复中发布了示例。在这一点上,我们与您最初的问题相距甚远,我给了您理由和解决方案。将字符串转换为整数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
相关资源
最近更新 更多