【问题标题】:Record audio from smartphone with Cordova使用 Cordova 从智能手机录制音频
【发布时间】:2016-03-07 13:28:48
【问题描述】:

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

所以我使用 VS 2015 添加了插件 cordova-plugin-media-capture。
然后,我从文档中复制/粘贴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>

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

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

缺少 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)

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

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

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

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

    Android 模拟器不具备捕获音频的能力,但实际设备可能会提供这些能力。

    取自adroid.developer.com MediaRecorder documentation

    【讨论】:

    • 感谢您的回答。你知道另一个模拟器(以及哪个?)能够连接到我电脑的麦克风来模拟音频文件的录制吗?
    • 我不太确定,因为我没有尝试过...使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    相关资源
    最近更新 更多