android - 如何在原生 android 2019 中使用 WebRTC

标签 android kotlin webrtc webrtc-android

如何在 Android Studio 的 Kotlin 中设置 WebRTC?我找不到可行的解决方案。请提供详细信息。

最佳答案

许多在线示例都使用旧的 Android WebRTC api。过去几年发生了很多变化。以下示例是 Java 语言的,但应该与 Kotlin 类似。

首先,您需要请求摄像头和音频的权限。然后也许使用 findviewbyid 设置您的 View ,然后将您的ice 服务器添加到数组中:

List<PeerConnection.IceServer> peerIceServers = new ArrayList<>();   
     peerIceServers.add(PeerConnection.IceServer.builder("stun:stun1.l.google.com:19302").createIceServer());

然后初始化您的对等连接工厂。

DefaultVideoEncoderFactory defaultVideoEncoderFactory = new DefaultVideoEncoderFactory(eglBase.getEglBaseContext(), true, true);
        DefaultVideoDecoderFactory defaultVideoDecoderFactory = new DefaultVideoDecoderFactory(eglBase.getEglBaseContext());

        PeerConnectionFactory.InitializationOptions initializationOptions =
                PeerConnectionFactory.InitializationOptions.builder(this)
                        .createInitializationOptions();
        PeerConnectionFactory.initialize(initializationOptions);

        PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
        factory = PeerConnectionFactory.builder()
                .setVideoEncoderFactory(defaultVideoEncoderFactory)
                .setVideoDecoderFactory(defaultVideoDecoderFactory)
                .setOptions(options)
                .createPeerConnectionFactory();

然后您可以初始化摄像头和音频以及信号客户端。

看着这个example在 Java 中可能会有所帮助:

关于android - 如何在原生 android 2019 中使用 WebRTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58268613/

相关文章:

android - 在 TextView 的选定状态中包含父填充

android - 为什么AndroidManifest.xml文件有多余的android :label attributes?

android - ImageSwitcher 上的图像无法居中对齐

kotlin - 从 init block 中的方法初始化 Kotlin 中的 val

iOS 使用 webrtc 和 Call Kit 保持后没有音频

node.js - 如何使用 npm 在项目中保持 WebRTC adapter.js 更新

安卓操作栏: collapsible SearchView with action button

android - CircleCI Android构建因Gradle 6.4失败

java - 我无法向 android studio 中的 color.xml 文件添加颜色

Node.js WebRTC 客户端