简体   繁体   English

Android WebView 和 webrtc hello world 示例

[英]Android WebView and webrtc hello world example

The code I'm pasting below was built with Android Studio and tested on an Android smartphone.我在下面粘贴的代码是用 Android Studio 构建的,并在 Android 智能手机上进行了测试。 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.我还写了一个 Java 变体而不是 Kotlin,一切都按预期工作。

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.如果我在 Chrome 中加载它,代码中提到的 webrtc 测试站点工作正常。

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"?那么,即使我在“Android 操作系统应用程序设置”中启用麦克风和摄像头权限,为什么我的 webview“测试应用程序”也无法访问任何媒体设备?

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谢谢

I think I need to use the WebChromeClient if I want webrtc to work.如果我想让 webrtc 工作,我想我需要使用 WebChromeClient。

Something like this seems to work for me in my MainActivity class:在我的 MainActivity 类中,这样的事情似乎对我有用:

myWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                request.grant(request.getResources());
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM