需求背景
當(dāng)客戶發(fā)貨或者收貨時(shí)?需要上傳開箱或者封箱視頻,直接選擇相冊視頻上傳速度太慢,現(xiàn)在隨便一個(gè)視頻都是幾百M(fèi)的,流量費(fèi)很貴,所以就選擇使用?直播推流的方式?實(shí)現(xiàn)視頻上傳。
app開發(fā),推薦使用nvue做直播,比使用vue的優(yōu)勢有:
nvue也可一套代碼編譯多端。nvue的cover-view比vue的cover-view更強(qiáng)大,在視頻上繪制元素更容易。如果只考慮App端的話,不用cover-view,任意組件都可以覆蓋live-pusher組件,因?yàn)閚vue沒有層級問題。若需要視頻內(nèi)嵌在swiper里上下滑動(類抖音、映客首頁模式),App端只有nvue才能實(shí)現(xiàn) 當(dāng)然nvue相比vue的壞處是css寫法受限,如果只開發(fā)微信小程序,不考慮App,那么使用vue頁面也是一樣的。
- 創(chuàng)建 live-pusher 上下文 livePusherContext 對象
onReady() {// 注意:需要在onReady中 或 onLoad 延時(shí)this.context = uni.createLivePusherContext('livePusher', this);//獲取手機(jī)信息,并設(shè)置高度和寬度try {const res = uni.getSystemInfoSync();this.width = res.windowWidth;this.height = res.windowHeight;} catch (e) {throw Error(e)}},
- 獲取請求url寬高自適應(yīng)全屏?注:安卓手機(jī)需要主動啟動攝像頭
onShow() {//開啟預(yù)覽uni.getStorage({key: 'livepushurl',success:(res) =>{const { url,id} = res.datathis.livepushurl= urlthis.id = id}});if(systemInfo.platform == 'android'){this.startPreview();}this.startTime()},
- onHide?處理?當(dāng)用戶切屏出去時(shí)?live-pusher會有bug?黑屏等問題,需要進(jìn)行處理,當(dāng)前處理3秒以內(nèi)不保存頁面后退。3秒以上進(jìn)行保存頁面后臺
onHide() {if(!this.showTime){if (this.count <= 3) {var page = getCurrentPages();var prevPage = page[page.length - 2];prevPage.$vm.closePusher(this.id);uni.navigateBack({delta: 1,});}else{this.stop()var page = getCurrentPages();var prevPage = page[page.length - 2];prevPage.$vm.finishPusher(this.id);uni.navigateBack({delta: 1,});}}},
- HTML?代碼塊? nvue?設(shè)置class樣式不生效?使用style?行內(nèi)樣式 (width height)
<view class="content"><live-pusher id="livePusher" ref="livePusher" :style="{width: width + 'px',height:height + 'px'}" :url="livepushurl":mode="mode" :muted="false":enable-camera="true" :auto-focus="true" :beauty="beauty" :whiteness="whiteness" aspect="9:16" enable-mic="true"device-position="back"@statechange="statechange" @netstatus="netstatus" @error="error"></live-pusher><view class="timekeeping" style="width: 200rpx;" :style="{ top: iStatusBarHeight + 'px'}"><text class="timekeeping-time" style="color: #FFFFFF;">{{time}}</text></view><view class="pusher-handle"><view class="pusher-handle-btn" @click="switchCamera"><image style="width: 68rpx;height: 68rpx;" src="../../static/images/live-pusher-switch.png" resize="cover"></image></view><view class="pusher-handle-btn pusher-handle-center" @click="showConfirmModal"><image style="width: 112rpx;height: 112rpx;" src="../../static/images/live-pusher-finish.png" resize="cover"></image></view><view class="pusher-handle-btn" @click="canceLlivePusher"><image style="width: 68rpx;height: 68rpx;" src="../../static/images/live-pusher-close.png" resize="cover"></image></view></view><view class="countdown" v-if="showTime"><text class="countdown-text">{{count}}</text></view></view>
let systemInfo = uni.getSystemInfoSync();export default {data() {return {width: '350px',height: '1500px',mode: 'FHD', //流視頻模式,可取值:SD(標(biāo)清), HD(高清), FHD(超清)beauty: 1, //美顏,取值范圍 0-9(iOS取值范圍為1) ,0 表示關(guān)閉whiteness: 2, // 美白,取值范圍 0-9(iOS取值范圍為1) ,0 表示關(guān)閉context: [],livepushurl: '',url: '',showTime: true,confirmContent: '是否確認(rèn)上傳當(dāng)前錄制?',cancleContent: '是否確認(rèn)退出當(dāng)前錄制?',showConfirm: false,showCancel: false,id: null,time: '00:00:00',iStatusBarHeight: 32,count: 0,};},onReady() {// 注意:需要在onReady中 或 onLoad 延時(shí)this.context = uni.createLivePusherContext('livePusher', this);//獲取手機(jī)信息,并設(shè)置高度和寬度try {const res = uni.getSystemInfoSync();this.width = res.windowWidth;this.height = res.windowHeight;} catch (e) {throw Error(e)}},onLoad() {},onShow() {//開啟預(yù)覽uni.getStorage({key: 'livepushurl',success:(res) =>{const { url,id} = res.datathis.livepushurl= urlthis.id = id}});if(systemInfo.platform == 'android'){this.startPreview();}this.startTime()},onHide() {if(!this.showTime){if (this.count <= 3) {var page = getCurrentPages();var prevPage = page[page.length - 2];prevPage.$vm.closePusher(this.id);uni.navigateBack({delta: 1,});}else{this.stop()var page = getCurrentPages();var prevPage = page[page.length - 2];prevPage.$vm.finishPusher(this.id);uni.navigateBack({delta: 1,});}}},methods: {async geturl() {try {const res = await getUrl({order_id: this.order_id,type: this.type})this.url = res.push.rtmpthis.id = res.idthis.startTime()} catch (e) {}},startTime() {const TIME_COUNT = 5;if (!this.timer) {this.count = TIME_COUNT;this.timer = setInterval(() => {if (this.count > 0 && this.count <= TIME_COUNT) {this.count--;} else {this.showTime = false;clearInterval(this.timer)this.timer = null;this.start()if(systemInfo.platform == 'ios'){this.switchCamera()}this.timekeeping()}}, 1000)}},timekeeping() {// var count = 0;if (!this.timerInter) {this.timerInter = setInterval(() => {var h = parseInt(this.count / 60 / 60);var m = parseInt(this.count / 60) % 60;var s = parseInt(this.count ) % 60;h = h < 10 ? '0' + h : h;m = m < 10 ? '0' + m : m;s = s < 10 ? '0' + s : s;this.time = h + ':' + m + ':' + s ;this.count += 1;}, 1000)}},statechange(e) {console.log('狀態(tài)變化事件:' + JSON.stringify(e));},netstatus(e) {console.log('網(wǎng)絡(luò)狀態(tài)通知事件:' + JSON.stringify(e));},error(e) {console.log('渲染錯(cuò)誤事件:' + JSON.stringify(e));},start() {this.context.start({success: a => {console.log('開始推流:' + JSON.stringify(a));},error: err => {console.log(err)}});},snapshot() {this.context.snapshot({success: e => {console.log('快照:' + JSON.stringify(e));}});},resume() {this.context.resume({success: a => {console.log('恢復(fù)推流:' + JSON.stringify(a));}});},pause() {this.context.pause({success: a => {console.log('暫停推流:' + JSON.stringify(a));}});},stop() {this.context.stop({success: a => {console.log('停止推流:' + JSON.stringify(a));}});},switchCamera() {this.context.switchCamera({success: a => {console.log('切換前后攝像頭:' + JSON.stringify(a));}});},startPreview() {this.context.startPreview({success: a => {console.log('開啟攝像頭預(yù)覽:' + JSON.stringify(a));}});},stopPreview() {this.context.stopPreview({success: a => {console.log('關(guān)閉攝像頭預(yù)覽:' + JSON.stringify(a));}});},showConfirmModal() {if (this.count <= 3) {uni.showToast({title: '上傳視頻不能低于3秒',icon: "none",duration: 2000,});return}uni.showModal({title: '提示',content: '是否確認(rèn)上傳當(dāng)前錄制?',success: (res)=> {if (res.confirm) {clearInterval(this.timerInter);uni.showLoading({title: '提交中'})setTimeout(()=>{uni.hideLoading()this.stop()var page = getCurrentPages();var prevPage = page[page.length - 2];prevPage.$vm.finishPusher(this.id);uni.navigateBack({delta: 1,});},2000)} else if (res.cancel) {console.log('用戶點(diǎn)擊取消');}}});// this.showConfirm = true},canceLlivePusher(){uni.showModal({title: '提示',content: '是否確認(rèn)退出當(dāng)前錄制?',success: (res)=> {if (res.confirm) {this.stop()var page = getCurrentPages();var prevPage = page[page.length - 2];prevPage.$vm.closePusher(this.id);uni.navigateBack({delta: 1,});} else if (res.cancel) {console.log('用戶點(diǎn)擊取消');}},})}}};
參數(shù)說明、回調(diào)方法可參考?文檔
總結(jié)
以上是生活随笔為你收集整理的uni app 使用live-pusher录制视频的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。