微信小程序有本身的推拉流組件以及API因此用小程序作直播仍是很方便的,須要注意的是目前只支持rtmp和flv格式.
話很少說先貼代碼php
第一部分是wxml文件的小程序
主要有一個拉流(live-player)和一個推流(pusher)微信小程序
1 <cover-view hidden="{{hideall}}" class="darg" style=" top:{{top}}px;left:{{left}}px;" bindtouchmove="setTouchMove"> 2 分數:{{mark}} 3 </cover-view> 4 <view class="page-body"> 5 <view class="page-section"> 6 <view class='live-player'> 7 <live-player class="live-body-player" id="player" src="rtmp://live.hkstv.hk.lxdns.com/live/hks" mode="live" autoplay bindstatechange="statechange" bindtouchstart="show_pusherC" binderror="error" object-fit="fillCrop" bindtouchend="touchend" /> 8 <cover-view hidden='{{!pusher_c}}' class='inner'> 9 <!---rtmp://10.206.208.1/myapp/mystream--> 10 <cover-image bindtap="bindStart" class="live-body-button" src="../../image/play3.png"></cover-image> 11 <cover-image bindtap="pusherPause" class="live-body-button" src="../../image/pause.png"></cover-image> 12 <cover-image bindtap="pusherStop" class="live-body-button" src="../../image/stop.png"></cover-image> 13 <cover-image bindtap="pusherResume" class="live-body-button" src="../../image/resume.png"></cover-image> 14 <cover-image bindtap="pusherSwitchCamera" class="live-body-button" src="../../image/camera.png"></cover-image> 15 </cover-view> 16 </view> 17 <view> 18 <live-pusher id="pusher" class="live-body-pusher" url="rtmp://28618.livepush.myqcloud.com/live/28618_789af258d8?bizid=28618&txSecret=ca539a6a284807e6e90964e389eaae79&txTime=5B82CE7F" mode="SD" autopush bindstatechange="statechange" aspect="3:4" /> 19 </view> 20 21 </view> 22 </view> 23 <view class='voide_control'> 24 <video id="myVideo" src="http://yzy.tunnel.qydev.com/images/upload/video/20180609/1528547498007.mp4" class='video_player'></video> 25 </view>
第二部分js服務器
onReady(res) { this.ctx = wx.createLivePusherContext('pusher'); this.video = wx.createVideoContext("myVideo"); }, statechange(e) { console.log('live-pusher code:', e.detail.code) // if (e.detail.code == 2001) { if(true){ this.video.play(); } }, pusherStart() { this.ctx.start({ success: res => { console.log('start success') }, fail: res => { console.log('start fail') } }) }, pusherPause() { this.ctx.pause({ success: res => { console.log('pause success') }, fail: res => { console.log('pause fail') } }) }, pusherStop() { this.ctx.stop({ success: res => { console.log('stop success') }, fail: res => { console.log('stop fail') } }) }, pusherResume() { this.ctx.resume({ success: res => { console.log('resume success') }, fail: res => { console.log('resume fail') } }) }, pusherSwitchCamera() { this.ctx.switchCamera({ success: res => { console.log('switchCamera success') }, fail: res => { console.log('switchCamera fail') } }) }, touchend: function(e) { var that = this; setTimeout(function() { that.setData({ "pusher_c": false }) }, 3000) }, show_pusherC: function() { var that = this; that.setData({ "pusher_c": true }) }, //分數 onShow: function() { wx.connectSocket({ url: 'test.php' }) wx.onSocketOpen(function (res) { console.log('WebSocket鏈接已打開!') }) wx.onSocketError(function (res) { console.log('WebSocket鏈接打開失敗,請檢查!') }) wx.onSocketMessage(function (res) { console.log('收到服務器內容:' + res.data) }) var that = this; setTimeout(function () { setInterval(function () { var mark = Math.round(Math.random() * 20)+60; that.setData({ "mark": mark }) }, 1000) }, 3000) }, })
另外你們可能在作開發時沒有直播源進行測試微信
在這裏告訴你們一個測試的方法:app
你們能夠登錄騰訊雲直播在產品裏找到直播,dom
而後使用騰訊的直播服務進行測試ide