前言
小程序的实时音视频播放需要先去微信开发者平台开通权限,「开发」-「接口设置」中自助开通该组件权限。
类目属性如下:
组件属性如下:
orientation子属性:
local-mirror子属性:
合法值 | 说明 |
auto | 前置摄像头镜像,后置摄像头不镜像 |
enable | 前后置摄像头均镜像 |
disable | 前后置摄像头均不镜像 |
一、实时音视频录制
1.js代码
Page({ onReady(res) { this.ctx = wx.createLivePusherContext('pusher') }, statechange(e) { console.log('live-pusher code:', e.detail.code) }, bindStart() { this.ctx.start({ success: res => { console.log('start success') }, fail: res => { console.log('start fail') } }) }, bindPause() { this.ctx.pause({ success: res => { console.log('pause success') }, fail: res => { console.log('pause fail') } }) }, bindStop() { this.ctx.stop({ success: res => { console.log('stop success') }, fail: res => { console.log('stop fail') } }) }, bindResume() { this.ctx.resume({ success: res => { console.log('resume success') }, fail: res => { console.log('resume fail') } }) }, bindSwitchCamera() { this.ctx.switchCamera({ success: res => { console.log('switchCamera success') }, fail: res => { console.log('switchCamera fail') } }) } })
2.wxml代码
<view class="page-body"> <view class="page-section tc"> <live-pusher id="pusher" url="https://domain/push_stream" mode="RTC" autopush bindstatechange="statechange" /> <view class="btn-area"> <button bindtap="bindStart" class="page-body-button" type="primary">播放推流</button> <button bindtap="bindPause" class="page-body-button" type="primary">暂停推流</button> <button bindtap="bindStop" class="page-body-button" type="primary">停止推流</button> <button bindtap="bindResume" class="page-body-button" type="primary">恢复推流</button> <button bindtap="bindSwitchCamera" class="page-body-button" type="primary">切换前后摄像头</button> </view> </view> </view>
3.效果