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

Record audio from smartphone with Cordova

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

所以我使用 VS 2015 添加了插件 cordova-plugin-media-capture
然后,我从文档 copy/paste 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>

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

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

missing exec:Capture.captureAudio

TypeError: emulator[service][action] is not a function
at module.exports.exec (ripple.js:41)
at _capture (capture.js:52)
at Capture.captureAudio (capture.js:71)
at captureAudio (index.html:60)
at HTMLButtonElement.onclick (index.html:91)

我不太明白问题出在哪里...

谢谢!

您正在尝试使用 ripple 模拟器测试您的应用,它只是一个模拟器,不会模拟所有设备功能,例如录制音频文件。另见

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

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

The Android Emulator does not have the ability to capture audio, but actual devices are likely to provide these capabilities.

取自adroid.developer.com MediaRecorder documentation