2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cordovaで音声入力する。

Last updated at Posted at 2019-10-02

UIWebViewではnavigator.mediaDevicesが取得できず、audio inputが出来ない。
cordova-plugin-audioinputを使う。

導入

cordova plugin add cordova-plugin-audioinput@1.0.1

サンプルコードが現時点で最新の1.0.2では動かなかったため、1.0.1を入れた。

Blob形式でデータを取得する流れ

  • 公式デモソースが教えてくれる。

Permissionを取る

準備

document.getElementById("startCapture").addEventListener("click", startCapture);

// 送られてくるデータを貯める
var onDeviceReady = function () {
    if (window.cordova && window.audioinput) {
        initUIEvents();

        consoleMessage("Use 'Start Capture' to begin...");

        // Subscribe to audioinput events
        //
        window.addEventListener('audioinput', onAudioInputCapture, false);
        window.addEventListener('audioinputerror', onAudioInputError, false);
    }
    else {
        consoleMessage("cordova-plugin-audioinput not found!");
        disableAllButtons();
    }
};

onAudioInput内部で、音声ストリーミングを取得する

開始

audioinput.start({audioSourceType: 0})

終了


//停止
audioinput.stop();
    
// waveへ変換する    
consoleMessage("Encoding WAV...");
var encoder = new WavAudioEncoder(captureCfg.sampleRate, captureCfg.channels);
encoder.encode([audioDataBuffer]);

consoleMessage("Encoding WAV finished");

var blob = encoder.finish("audio/wav");
2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?