Get mic audio in android. AudioContext

2019-07-16 04:36发布

问题:

(For some reason I can't get an answer to this problem...)

Hello. I need to access android microphone. I made a tuner app in the web using this: https://github.com/cwilso/PitchDetect. Works just fine.

However when I build the app to android using intelXDK and cordova plugins I can't get any mic input. I am not sure if I need to use this: https://github.com/edimuj/cordova-plugin-audioinput. Seems like the right way to get the audioContext in android. Plus it shows a warning when installing the app saying that it needs to give authorization. Probably it is the right path, no?

Anyway can someone help me with this? Any idea why, despite I get logs saying that input is enabled I can't get any audio? (microphone is working ofc)

回答1:

I'm the creator of cordova-plugin-audioinput, a Cordova plugin which gives you access to the raw audio data from the microphone. Without modifying the PitchDetect library, you cannot use it together with the audioinput plugin. The PitchDetect library, relies on the getUserMedia API, which isn't supported on older Android webviews (<5.0), and is not supported at all on any of the iOS webviews. The cordova-plugin-audioinput, on the other hand, was specifically created to enable microphone access on both iOS and on older Android webviews.

A quick way that could solve your specific problem on Android:

The Intel XDK supports building Android apps with the Crosswalk webview, which enables you to use a newer version of Blink (The open-source version of Chrome), that supports both the Web Audio API and the getUserMedia API.

To enable Crosswalk on your builds, go to the Build Settings screen for Android in the Intel XDK and ensure that the "Optimize with Crosswalk" checkbox is checked. For more information about using Crosswalk with the Intel XDK: https://software.intel.com/en-us/xdk/blog/what-is-crosswalk.

I believe that the app also will need permission to access the microphone, so enter "android.permission.RECORD_AUDIO" in the "Add permission" field of the Intel XDK build configuration.

Using cordova-plugin-audioinput together with PitchDetect

If you, on the other hand, want to use the cordova-plugin-audioinput together with the PitchDetect library (for example if you want your app to also work in iOS, which doesn't support the getUserMedia API), you must do some modifications; specifically, you would need to change the toggleLiveInput function of PitchDetect, so that it doesn't rely on getUserMedia, but instead starts (and stops) the audioinput plugin and connects the audioinput plugin to the PitchDetect analyser:

audioinput.start({
    streamToWebAudio: true,
    audioContext: audioContext // To ensure that the audioinput plugin uses the same audioContext as the PitchDetect library
});
analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
audioinput.connect(analyser);
updatePitch();

Please note that the above example does still rely on the Web Audio API, which has limited support on older Android versions; Android 5+ should work fine though. For more information about Web Audio API support on different browsers: http://caniuse.com/#feat=audio-api.

I hope this helps and good luck!