知識
不管是網站,軟件還是小程序,都要直接或間接能為您產生價值,我們在追求其視覺表現的同時,更側重于功能的便捷,營銷的便利,運營的高效,讓網站成為營銷工具,讓軟件能切實提升企業(yè)內部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
微信小程序-實現錄制視頻(附部分代碼)
發(fā)表時間:2020-9-23
發(fā)布人:葵宇科技
瀏覽次數:143
項目中,需要客戶錄制一段視頻,上傳到服務器,找了很久,終于實現了這個功能。微信小程序有兩種方式可以實現錄制視頻。
1.使用相機的CameraContext.startRecord
2.使用官方API:wx.chooseVideo
方法一
wxml
<view class="video">
<camera wx:if="{{videoSrc.length === 0}}" device-position="font" flash="off" binderror="error" style="width: {{cameraWidth}}px;height: 442rpx;">
</camera>
<video style="width: {{cameraWidth}}px; height: 442rpx;" wx:else src="{{videoSrc}}" controls></video>
</view>
<view class="btn" id='btn-photo' bindtouchstart="handleTouchStart" bindtouchend="handleTouchEnd" bindlongpress="handleLongPress">按住錄制</view>
js部分代碼
onLoad: function (options) {
//調用設置相機大小的方法
this.setCameraSize();
this.ctx = wx.createCameraContext();
},
/**
* 獲取系統(tǒng)信息 設置相機的大小適應屏幕
*/
setCameraSize() {
//獲取設備信息
const res = wx.getSystemInfoSync();
//獲取屏幕的可使用寬高,設置給相機
this.setData({
cameraHeight: res.windowHeight,
cameraWidth: res.windowWidth
})
console.log(res)
},
/**
* 開始錄像的方法
*/
startShootVideo() {
this.setData({
videoSrc: ''
})
console.log("========= 調用開始錄像 ===========")
let that = this
this.ctx.startRecord({
timeoutCallback: () => {
},
success: (res) => {
},
fail() {
wx.showToast({
title: '錄像失敗',
icon: 'none',
duration:4000
})
console.log("========= 調用開始錄像失敗 ===========")
}
})
},
/**
* 結束錄像
*/
stopShootVideo() {
wx.hideLoading();
// console.log("========= 調用結束錄像 ===========")
this.ctx.stopRecord({
compressed: true, //壓縮視頻
success: (res) => {
console.log(res)
this.setData({
videoSrc: res.tempVideoPath
})
},
fail() {
wx.showToast({
title: '錄像失敗',
icon: 'none',
duration:4000
})
console.log("========= 調用結束錄像失敗 ===========")
}
})
},
//touch start 手指觸摸開始
handleTouchStart: function (e) {
this.setData({
startTime: e.timeStamp
})
},
//touch end 手指觸摸結束
handleTouchEnd: function (e) {
// wx.hideLoading();
let endTime = e.timeStamp;
//判斷是點擊還是長按 點擊不做任何事件,長按 觸發(fā)結束錄像
if (endTime - this.data.startTime > 350) {
//長按操作 調用結束錄像方法
this.stopShootVideo();
} else {
this.setData({
textFlag: ''
})
}
},
/**
* 長按按鈕進行錄像
*/
handleLongPress: function (e) {
// 長按方法觸發(fā),調用開始錄像方法
this.startShootVideo();
},
注:由于官方限制,只能錄制30秒。
方法二
在js中
wx.chooseVideo({
sourceType: ['album','camera'],
maxDuration: 60,
camera: 'back',
success(res) {
console.log(res.tempFilePath)
}
})
使用wx.chooseVideo拍攝視頻或從手機相冊中選視頻,拍攝界面不能自定義。
如果有什么不清楚的地方,歡迎私信哦.
具體參數說明請參考官方文檔
https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseVideo.html(鏈接地址)
作者:IU憨憨
鏈接:https://www.jianshu.com/p/1fdbe12ce477
來源:簡書
著作權歸作者所有。商業(yè)轉載請聯系作者獲得授權,非商業(yè)轉載請注明出處。