简体   繁体   中英

How to access ios core audio(AudioQueue) on Nativescript

I'm new to nativescript and developing ios version app now. I have some trouble handling Audio Queue callback.

I need capturing raw audio buffer data via device microphone.(evaluating input level in millisecond order)1 To do this, I'm trying to call ios native AudioToolBox/AudioQueueNewInput API from my custom plugin.

Finally, it run with no error, but the callback "inCallbackProc" is never fired...

Here is my code.

myplugin.ios.js

var kSamplingRate = 44100;
var inUserData = null;
var inCallbackRunLoopMode = kCFRunLoopDefaultMode;
var inFlags = 0;
//var outAQ = AudioQueueRef.alloc().init(); // error
//var outAQ = new OpaqueAudioQueue(); //error
var outAQ;
var inFormat = 
    new AudioStreamBasicDescription(
    kSamplingRate,
    kAudioFormatLinearPCM,
    kLinearPCMFormatFlagIsFloat,
    4,
    1,
    4,
    1,
    8,
    0
);

var buffers = [null, null, null];

var audiomodule = {
    inCallbackProc : function(
        inUserData, 
        inAQ, 
        inBuffer, 
        inStartTime, 
        inNumberPacketDescriptions, 
        inPacketDescs 
        ){
            console.log("callbacked");
        }
    ,record: function(){
        console.log("recordstarting");

        AudioQueueNewInput (
         inFormat, 
         this.inCallbackProc.copy,
         inUserData, 
         CFRunLoopGetCurrent(),
         inCallbackRunLoopMode, 
         inFlags, 
         outAQ
         );
        for (i in [0, 1, 2]) {
            AudioQueueAllocateBuffer(outAQ, 256, buffers[i])
            AudioQueueEnqueueBuffer(outAQ, buffers[i], 0, null)
           }

    AudioQueueStart(outAQ, null);
    }

};

module.exports = audiomodule;

app.js

var MyPlugin = require("myplugin");

exports.loaded = function(args) {
    var page = args.object;
    if (page.ios) {
        MyPlugin.record();
    }
};

When Executing this code, I can find "recordstarting" in console log with no error, but can't find "callbacked".

I have read this doc. NativeScript Doc - Marshaling

I appreciate anyone help or comment.

I decided to create my own plugin.
My own Objective-c library wraps native AudioRecord(Core Audio) API.
And own plugin js exposes the library functions.
Finally, it works, callback work also.

mylibrary(Objective-c)

@property (nonatomic, copy) void (^completionHandler)(NSString *result);

myplugin.ios.js

mylibrary.completionHandler = (
function(response){
    console.log(response);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM