WebRTC 开启视频

<!DOCTYPE html>
<html lang="ZH-CN">
<head>
  <meta charset="utf-8">
  <title>web RTC 测试</title>
  <style>
    .booth {
      width:400px;
     
      background:#ccc;
      border: 10px solid #ddd;
      margin: 0 auto;
    }
  </style>
</head>
<body>
    <article>
      <section>
        <video id="video"></video>
      </section>
      <section>
        <audio id="audio"></audio>
      </section>
      <button id="btn">拍照</button>
      <section>
        <canvas id="canvas"></canvas>
      </section>
      <section><img src="" alt="" id="img"></section>
    </article>

    <article>
      <header>相关知识</header>
      <h2>获取用户媒体:</h2>
      <p>
        <code>navigator.mediaDevices.getUserMedia({video: true, audio: true})  // 异步操作</code>
      </p>

      <h2>枚举媒体数:video,audio输入输出设备</h2>
      <p><code>navigator.mediaDevices.enumerateDevices()  // 异步操作</code></p>
    </article>
  <script>
    let convas = document.querySelector("#canvas"); //
    let video = document.querySelector("#video");
    let audio = document.querySelector("audio");
    let img = document.querySelector("#img");
    let btn = document.querySelector("button");
    let context = canvas.getContext('2d');
    let width = 320; //视频和canvas的宽度
    let height = 0; //
    let streaming = false; // 是否开始捕获媒体
    
    // 获取用户媒体,包含视频和音频
    navigator.mediaDevices.getUserMedia({video: true, audio: true})
      .then(stream => {
      video.srcObject = stream; // 将捕获的视频流传递给video  放弃window.URL.createObjectURL(stream)的使用
      video.play(); //  播放视频
      audio.srcObject = stream;
      audio.play();
    });
    
    
    function tackcapture() {
      // 需要判断媒体流是否就绪
      if(streaming){
          context.drawImage(video, 0, 0, 350, 200);// 将视频画面捕捉后绘制到canvas里面
          img.src = canvas.toDataURL('image/png');// 将canvas的数据传送到img里
      }
    
    }
    
    btn.addEventListener('click',tackcapture,false); // 按钮点击事件
    
    // 监听视频流就位事件,即视频可以播放了
    video.addEventListener('canplay', function(ev){
          if (!streaming) {
            height = video.videoHeight / (video.videoWidth/width);
          
            video.setAttribute('width', width);
            video.setAttribute('height', height);
            canvas.setAttribute('width', width);
            canvas.setAttribute('height', height);
            streaming = true;
          }
        }, false);
  </script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 上打开 WebRTC 视频流,你需要使用 WebRTC 库和 Android 原生代码来实现。以下是一个示例代码,可以帮助你开始: 1. 导入 WebRTC 库: 在你的 Android 项目中,在 app/build.gradle 文件中添加以下依赖项: ``` implementation 'org.webrtc:google-webrtc:1.0.+' ``` 2. 创建 PeerConnectionFactory: ``` PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions.builder(context).createInitializationOptions()); PeerConnectionFactory peerConnectionFactory = PeerConnectionFactory.builder().createPeerConnectionFactory(); ``` 3. 创建 VideoCapturer: ``` VideoCapturer videoCapturer = createCameraCapturer(new Camera1Enumerator(false)); ``` 4. 创建 VideoSource: ``` SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", null); VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast()); videoCapturer.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver()); videoCapturer.startCapture(1280, 720, 30); ``` 5. 创建 VideoTrack: ``` VideoTrack videoTrack = peerConnectionFactory.createVideoTrack("video", videoSource); ``` 6. 创建 PeerConnection: ``` PeerConnection.RTCConfiguration configuration = new PeerConnection.RTCConfiguration(Arrays.asList(new PeerConnection.IceServer("stun:stun.l.google.com:19302"))); PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(configuration, new CustomPeerConnectionObserver()); ``` 7. 将 VideoTrack 添加到 PeerConnection 中: ``` peerConnection.addTrack(videoTrack); ``` 8. 创建 VideoRenderer: ``` SurfaceViewRenderer localView = findViewById(R.id.local_view); localView.init(surfaceTextureHelper.getSurfaceTexture(), context); localView.setMirror(true); VideoRenderer localRenderer = new VideoRenderer(localView); videoTrack.addRenderer(localRenderer); ``` 9. 创建 Offer: ``` peerConnection.createOffer(new CustomSdpObserver() { @Override public void onCreateSuccess(SessionDescription sessionDescription) { peerConnection.setLocalDescription(new CustomSdpObserver(), sessionDescription); // 发送 Offer 到远端 } }, new MediaConstraints()); ``` 这就是一个简单的 WebRTC 视频流应用的示例代码。你需要根据你的具体需求来进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值