Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No audio in WebRTC over WebView in Android app

I developed a simple Android app that wrap a WebView to connect to apprtc.appspot.com. The WebRTC session is establish successfully, video streams are shown in both the app and the peer (a Chrome browser on a Mac,) the audio can be heard on the app, but the Mac does not receive any audio. chrome://webrtc-internals on the Mac's Chrome browser shows no error. WebRTCing from Chrome in the Android device to the Chrome in the Mac works fine with audio. I also wrote a test activity in the app to use MediaRecorder and MediaPlayer that successfully captured and played back audio.

My permission set up looks like

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.audio.low_latency" />
<uses-feature android:name="android.hardware.audio.pro" />
<uses-feature android:name="android.hardware.microphone" android:required="true"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="true" />

The app codes look like

    int permission;

    permission = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
    if (permission != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.RECORD_AUDIO)) {
            // Show an expanation to the user *asynchronously* -- don't block
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.RECORD_AUDIO},
                    MY_PERMISSIONS_REQUEST_AUDIO);
        }
    }
    // ... similar camera permission request ...

    WebView webView = (WebView) findViewById(R.id.web_view);
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setDatabaseEnabled(true);
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onPermissionRequest(PermissionRequest request) {
            request.grant(request.getResources());
        }
    });
    webView.loadUrl("https://appr.tc/r/my-room");

What goes wrong?

like image 232
teddy Avatar asked Jun 16 '16 00:06

teddy


2 Answers

Adding <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> in the permission solved the problem for me.

like image 58
Sunil Lama Avatar answered Sep 22 '22 14:09

Sunil Lama


Add:

settings.setMediaPlaybackRequiresUserGesture(false);
like image 44
David Custodio Avatar answered Sep 25 '22 14:09

David Custodio