captureAudio captureVideo

32 views
Skip to first unread message

Jamie Irvine

unread,
Apr 24, 2015, 1:09:07 PM4/24/15
to phon...@googlegroups.com
Hi

I'm experiencing a problem with the capture plugin for PhoneGap on WP8 - I'm using a Nokia Lumia 625 as the target device. The problem I'm having with both the captureAudio and captureVideo methods is that neither seem to trigger either the success of failure callbacks. Also in the code example below the try/catch is not triggering either.

Here is an example of the code:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
    capturevideo();
}

function capturevideo()
{
    try
    {
        function captureError(error)
        {
            alert("Video error");
        }

        function captureSuccess()
        {
            alert("Video Success");
        }

        navigator.device.capture.captureVideo(captureSuccess, captureError, {limit:1});
    }
    catch (error)
    {
        alert("Error "+error.message);
    }
}

I'd appreciate any help with this.

Jamie Irvine

unread,
Apr 27, 2015, 6:51:30 AM4/27/15
to phon...@googlegroups.com
Adding to this:

It seems that the issue is being caused as there is no app on the device to handle the intent, which does seem odd. Anyhow, I've moved over to using the Media plugin for audio capture and have now ran into another issue! I'm getting FOUR callbacks to the media success function; an in turn four more for each NEW media I create (so 4 then 8 then 12 then 16 etc). Media plugin version is 0.2.16. Phonegap version is 3.6.3-0.22.6. Here is an example of the code I am using:

var thismedia = null;
var audiorecording = false;
var audioplaying = false;

function onDeviceReady()
{
    recordaudiostart();
}

function mediasuccess()
{
    alert("Got media success callback");
    audiorecording = false;
}

function mediafailure(error)
{
    thismedia.release();
    audiorecording = false;
    alert("Got media failure callback");
}

//Media.MEDIA_NONE = 0;
//Media.MEDIA_STARTING = 1;
//Media.MEDIA_RUNNING = 2;
//Media.MEDIA_PAUSED = 3;
//Media.MEDIA_STOPPED = 4;
//Media.MEDIA_MSG = ["None", "Starting", "Running", "Paused", "Stopped"];

function audiostatus(status)
{
    try
    {
        switch(status)
        {
            case 1:
                alert("Audio starting");
                break;
            case 4:
                alert("Audio Stopped");
                break;
        }
    }
    catch(error)
    {
        alert("Error : "+error.message );
    }
}

function recordaudiostart()
{
    try
    {
        if(!audiorecording)
        {
            if(thismedia!==null)
            {
                thismedia.release();
            }
            var src = "myrecording.wav";
            thismedia = new Media(src, mediasuccess, mediafailure, audiostatus);
            thismedia.startRecord();
            audiorecording = true;
        }
        else
        {
            alert("Already recording!");
        }
    }
    catch(error)
    {
        alert("error "+error.message);
    }
}

function recordaudiostop()
{
    try
    {
        if(audiorecording)
        {
            alert("Stopping recording");
            thismedia.stopRecord();
        }
        else
        {
            alert("Not recording anything");
        }
    }
    catch(error)
    {
        alert("error "+error.message);
    }
}

Any help with this would be greatly appreciated.

Jamie Irvine

unread,
Apr 27, 2015, 11:46:02 AM4/27/15
to phon...@googlegroups.com
The result dispatch in stopRecordingAudio : DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId); is only being fired once (stuck a breakpoint in VS to check) yet resulting in four callbacks?

On Friday, April 24, 2015 at 6:09:07 PM UTC+1, Jamie Irvine wrote:

Jamie Irvine

unread,
Apr 27, 2015, 2:18:17 PM4/27/15
to phon...@googlegroups.com
Little bit more info, if anyone has any help to offer. So I'm seeing multiple callbacks from the Media Plugin (WP8 Nokia Lumia 625) on media Success function. I've tracked it down to the following:

(AudioPlayer.cs)
        private void InvokeCallback(int message, string value, bool removeHandler)
        {
            Debug.WriteLine("Invoke Callback!"+message.ToString()+" "+value.ToString()+" "+removeHandler.ToString());
            string args = string.Format("('{0}',{1},{2});", this.id, message, value);
            string callback = @"(function(id,msg,value){
                try {
                    console.log('callback function is being executed multiple times');
                    if (msg == Media.MEDIA_ERROR) {
                        value = {'code':value};
                    }
                    Media.onStatus(id,msg,value);
                }
                catch(e) {
                    console.log('Error calling Media.onStatus :: ' + e);
                }
            })" + args;
            this.handler.InvokeCustomScript(new ScriptCallback("eval", new string[] { callback }), false);
        }

This function is only being called the one time, as expected; however the javascript being defined as a string (in callback) above IS being executed multiple times. Just to note that the ResultHandler is being destroyed (as expected) and not being handled multiple times.

Any help with this would be appreciated.

On Friday, April 24, 2015 at 6:09:07 PM UTC+1, Jamie Irvine wrote:

Jamie Irvine

unread,
Apr 28, 2015, 8:07:41 AM4/28/15
to phon...@googlegroups.com
In case anyone else is finding the same problem - I decided to go back and try to figure out what was happening with the capture plugin, and finally got it working. The solution in my case is not a great solution but does get the capture plugin working. You will need to make a change to Cordova iteself; in CordovaLib/Commands/BaseCommands.cs:

Find the function: DetachHandlers

and replace with

        public void DetachHandlers()
        {
            this.OnCommandResult = null;
            this.OnCustomScript = null;
            foreach (string callbackId in new List<string>(ResultHandlers.Keys))
            {
                if (!callbackId.ToLower().Contains("capture"))
                {
                    RemoveResultHandler(callbackId);
                }
            }
        }



On Friday, April 24, 2015 at 6:09:07 PM UTC+1, Jamie Irvine wrote:

Jesse Monroy

unread,
Apr 30, 2015, 8:47:51 PM4/30/15
to phon...@googlegroups.com
Have your tried the "Full Example" in the documentation?
If you have, then you should post your code that is NOT working.
Also, since you did NOT post your code, I canNOT tell you if you forgot to put cordova.js in the <head>

Jesse
Reply all
Reply to author
Forward
0 new messages