代码 nvue
<template>
<view>
<live-pusher id="livePusher" :url="url" :enable-camera="enableCamera" mode="FHD"></live-pusher>
<button @click="startLive">开始推流(开始直播)</button>
<button @click="stopLive">结束推流</button>
</view>
</template>
<script>
export default {
data() {
return {
url: 'rtmp://192.168.1.112/live/zfy',
enableCamera: true,
context: null
};
},
created() {
this.context = uni.createLivePusherContext('livePusher', this);
this.context.switchCamera() // 摄像头切换(切换为后置)
this.context.startPreview() // 摄像头预览 (不加会黑屏)
},
methods: {
startLive() {
this.context.start({
success: a => {
console.log('livePusher.start:' + JSON.stringify(a));
}
});
},
stopLive() {
this.context.stop({
success: a => {
console.log(JSON.stringify(a));
}
});
}
}
};
</script>