【问题标题】:Android WebView and webrtc hello world exampleAndroid WebView 和 webrtc hello world 示例
【发布时间】:2020-11-18 13:07:39
【问题描述】:

我在下面粘贴的代码是使用 Android Studio 构建的,并在 Android 智能手机上进行了测试。只要我手动启用摄像头和麦克风权限,我很确定它工作正常。我还编写了一个 Java 变体而不是 Kotlin,并且一切都按预期工作。

但是,无论我重新启动设备多少次或确保没有其他正在运行的应用程序可能会使用摄像头或麦克风,我都完全无法再让这个应用程序运行。

如果我在 Chrome 中加载代码中提到的 webrtc 测试站点,它可以正常工作。

那么,即使我在“Android OS 应用设置”中启用了麦克风和摄像头权限,为什么我的 webview“测试应用”无法访问任何媒体设备?

就像我说的,我很确定它以前确实有效,但显然出了点问题。我只是想确保这里的代码是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")
    }
}

清单文件:

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

布局:

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

谢谢

【问题讨论】:

    标签: android webview webrtc


    【解决方案1】:

    如果我想让 webrtc 工作,我想我需要使用 WebChromeClient。

    这样的事情在我的 MainActivity 类中似乎对我有用:

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

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 2013-07-31
      • 1970-01-01
      相关资源
      最近更新 更多