使用Cordova从智能手机录制音频

时间:2016-03-07 13:28:48

标签: cordova audio cordova-plugins audio-recording ripple

非常新与Cordova,我正在尝试创建一个非常基本的应用程序来录制智能手机的音频。

所以我使用VS 2015添加了插件cordova-plugin-media-capture 然后,我从doc:

复制/粘贴this full example
<head>
<title>Capture Audio</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
//I just remove the following line in MY code, I don't know what it corresponds
<script type="text/javascript" charset="utf-8" src="json2.js"></script> 

<script type="text/javascript" charset="utf-8">
    // Called when capture operation is finished
    function captureSuccess(mediaFiles) {
        var i, len;
        for (i = 0, len = mediaFiles.length; i < len; i += 1) {
            uploadFile(mediaFiles[i]);
        }
    }

    // Called if something bad happens.
    function captureError(error) {
        var msg = 'An error occurred during capture: ' + error.code;
        navigator.notification.alert(msg, null, 'Uh oh!');
    }

    // A button will call this function
    function captureAudio() {
        // Launch device audio recording application,
        // allowing user to capture up to 2 audio clips
        navigator.device.capture.captureAudio(captureSuccess, captureError, {limit: 2});
    }

    // Upload files to server
    function uploadFile(mediaFile) {
        var ft = new FileTransfer(),
            path = mediaFile.fullPath,
            name = mediaFile.name;

        ft.upload(path,
            "http://my.domain.com/upload.php",
            function(result) {
                console.log('Upload success: ' + result.responseCode);
                console.log(result.bytesSent + ' bytes sent');
            },
            function(error) {
                console.log('Error uploading file ' + path + ': ' + error.code);
            },
            { fileName: name });
    }
</script>
</head>
<body>
    <button onclick="captureAudio();">Capture Audio</button> <br>
</body>

但是,当我运行此代码并单击“Capture Audio”按钮时,我得到以下结果:

capture.captureAudio fail

我在控制台中也出现以下错误:

  

缺少exec:Capture.captureAudio

     

TypeError:emulator [service] [action]不是函数
      在module.exports.exec(ripple.js:41)
      在_capture(capture.js:52)
      在Capture.captureAudio(capture.js:71)
      在captureAudio(index.html:60)
      在HTMLButtonElement.onclick(index.html:91)

我真的不明白问题出在哪里......

谢谢!

1 个答案:

答案 0 :(得分:2)

您正尝试使用纹波仿真器测试您的应用,这只是一个模拟器,并不会模拟所有设备功能,例如录制音频文件。另见this answer

尝试使用真实设备测试您的应用。

编辑:我不确定是否有任何模拟器可以通过您的PC硬件实际捕获声音。例如Android模拟器不能。

  

Android模拟器无法捕获音频,但实际设备可能会提供这些功能。

取自adroid.developer.com MediaRecorder documentation