<template>
<view>
<live-pusher id='livePusher' ref="livePusher" class="livePusher" :url="url"
mode="SD" :muted="true" :enable-camera="true" :auto-focus="true" :beauty="1" whiteness="2"
aspect="9:16" @statechange="statechange" @netstatus="netstatus" @error = "error"
</live-pusher>
<button class="btn" @click="start">开始推流</button>
<button class="btn" @click="pause">暂停推流</button>
<button class="btn" @click="resume">resume</button>
<button class="btn" @click="stop">停止推流</button>
<button class="btn" @click="snapshot">快照</button>
<button class="btn" @click="startPreview">开启摄像头预览</button>
<button class="btn" @click="stopPreview">关闭摄像头预览</button>
<button class="btn" @click="switchCamera">切换摄像头</button>
</view>
</template>
<script>
import md5 from '@/js_sdk/md5.js';
export default {
data() {
return {
url:'',
context:''
}
},
onReady() {
let that = this;
// 注意:需要在onReady中 或 onLoad 延时
that.context = uni.createLivePusherContext("livePusher", this);
setTimeout(() => {
//this.context = uni.createLivePusherContext('livePusher', this);
var secretdate= 'xxxxxxx'; //测试时此处需要更换
let date = Date.parse(new Date())/1000 + 21600;
//这是需要在腾讯云配置推流的地址,这里采用的是flv的视频格式
that.url = 'http://livepush.nync.com/live/123456789.flv?txSecret='+md5(secretdate+this.live_id+date.toString(16).toUpperCase())+'&txTime='+date.toString(16).toUpperCase();
that.start(0);
}, 1000)
},
methods: {
statechange(e) {
// console.log("statechange:",e);
},
netstatus(e) {
//console.log("netstatus:",e);
},
error(e) {
//console.log("error:",e);
},
start: function() {
this.context.start({
success: (a) => {
console.log("livePusher.start:" + JSON.stringify(a));
},
fail: a =>{
console.log("开启直播失败:" + JSON.stringify(a));
}
});
},
close: function() {
this.context.close({
success: (a) => {
console.log("livePusher.close:" + JSON.stringify(a));
}
});
},
snapshot: function() {
this.context.snapshot({
success: (e) => {
console.log(JSON.stringify(e));
}
});
},
resume: function() {
this.context.resume({
success: (a) => {
console.log("livePusher.resume:" + JSON.stringify(a));
}
});
},
pause: function() {
this.context.pause({
success: (a) => {
console.log("livePusher.pause:" + JSON.stringify(a));
}
});
},
stop: function() {
this.context.stop({
success: (a) => {
console.log(JSON.stringify(a));
}
});
},
switchCamera: function() {
this.context.switchCamera({
success: (a) => {
console.log("livePusher.switchCamera:" + JSON.stringify(a));
}
});
},
startPreview: function() {
this.context.startPreview({
success: (a) => {
console.log("livePusher.startPreview:" + JSON.stringify(a));
}
});
},
stopPreview: function() {
this.context.stopPreview({
success: (a) => {
console.log("livePusher.stopPreview:" + JSON.stringify(a));
}
});
}
}
}
</script>
刘超群 (作者)
代码都贴出来了
2022-04-01 15:45
刘超群 (作者)
官方提供的live-pusher示例代码就不行,以前可以,现在不行了,应该是这两个版本更新出现的问题
2022-04-01 15:46
天生DR
回复 刘超群: 代码格式 你没有贴好 看起来是乱的 你可以上传示例demo 压缩成zip 传附件
2022-04-06 11:03
刘超群 (作者)
回复 天生DR: 找到问题了,可以结帖
2022-04-06 14:33
天生DR
回复 刘超群: 好的
2022-04-06 17:41