Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView and webrtc hello world example

The code I'm pasting below was built with Android Studio and tested on an Android smartphone. I'm pretty sure it was working fine as long as I manually enabled camera and microphone permissions. I also wrote a Java variant instead of Kotlin and all worked as expected.

However, I am totally unable to make this app work anymore, no matter how many times I reboot my device or make sure there are no other running apps that might use the camera or mic.

The webrtc test site mentioned in the code works fine if I load it in Chrome.

So, any ideas why my webview "test app" cannot access any media device even if I enable microphone and camera permissions in "Android OS app settings"?

And like I said, I'm pretty sure it DID work before, but something went wrong, obviously. I just want to make sure here that the code is OK.

package org.me.test

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val WebView: WebView = findViewById(R.id.webview)
        WebView.settings.javaScriptEnabled = true
        WebView.loadUrl("https://test.webrtc.org")
    }
}

The manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.me.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.test">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

</manifest>

The layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Thanks

like image 807
user1936810 Avatar asked Mar 03 '26 10:03

user1936810


1 Answers

I think I need to use the WebChromeClient if I want webrtc to work.

Something like this seems to work for me in my MainActivity class:

myWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                request.grant(request.getResources());
            }
        });
like image 177
user1936810 Avatar answered Mar 05 '26 22:03

user1936810



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!