2

I'm using Media​Stream Recording API to record user's MIC. The code works fine on chrome windows but when it comes to android it just stops recording instantly after recording beep.

How can I make the API work on android devices?
Am I missing something or this is a bug with Web Audio API?

Here is the code:

  navigator.mediaDevices.getUserMedia({audio:true})
    .then(stream => {
        rec = new MediaRecorder(stream);
        rec.ondataavailable = e => {
            audioChunks.push(e.data);
            if (rec.state == "inactive"){
        let blob = new Blob(audioChunks,{type:'audio/x-mpeg-3'});
        recordedAudio.src = URL.createObjectURL(blob);

Then I just use these lines to start recording :

audioChunks = [];
rec.start();

And This one to stop and play:

rec.stop();
recordedAudio.autoplay = true;
Sara Ree
  • 3,417
  • 12
  • 48

1 Answers1

1

Chrome autoplay requires user interaction to play a sound.

Check this and this for javascript based workarounds.

Or if you are making an android app and using webview to load your page, try setting setMediaPlaybackRequiresUserGesture to false.

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("https://www.example.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setMediaPlaybackRequiresUserGesture(false);
Aparna
  • 572
  • 4
  • 9
  • Did you check the permissions? is there any error in Logcat? – Aparna Jun 07 '19 at 08:59
  • Ok... It actually is a voice recognition app (recognition works fine both on Android and Desktop) that when you say "My name is Jack" it'll record and playback your voice. – Sara Ree Jun 07 '19 at 09:42
  • It works fine on Windows chrome but when I try it on Android chrome I can hear start recording beep then, instantly recording ends... No time for recording on Android... – Sara Ree Jun 07 '19 at 09:48
  • Okay, I was assuming you were trying to make an android app. So if recording works fine, are you facing problem is with playback? – Aparna Jun 07 '19 at 10:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194588/discussion-between-aparna-and-sara-ree). – Aparna Jun 07 '19 at 10:34