小程序-实现录制视频(附部分代码)
⼩程序-实现录制视频(附部分代码)
项⽬中,需要客户录制⼀段视频,上传到服务器,了很久,终于实现了这个功能。⼩程序有两种⽅式可以
实现录制视频。
1.使⽤相机的CameraContext.startRecord
2.使⽤官⽅API:wx.chooseVideo
⽅法⼀
wxml
<view class="video">
<camera wx:if="{{videoSrc.length === 0}}" device-position="font" flash="off" binderror="error" >
</camera>
<video 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();
< = wx.createCameraContext();
},
/**
* 获取系统信息设置相机的⼤⼩适应屏幕
*/
setCameraSize() {
第三套人民币价格//获取设备信息
const res = wx.getSystemInfoSync();
//获取屏幕的可使⽤宽⾼,设置给相机
this.setData({
cameraHeight: res.windowHeight,
cameraWidth: res.windowWidth
})
console.log(res)
},
/
**
* 开始录像的⽅法
*/
startShootVideo() {
this.setData({
videoSrc: ''
})
交管123123违章查询
console.log("========= 调⽤开始录像 ===========")
全国最好的化妆学校
let that = this
timeoutCallback: () => {
},
success: (res) => {
},
fail() {
wx.showToast({
辽宁美食title: '录像失败',
icon: 'none',
duration:4000
})
console.log("========= 调⽤开始录像失败 ===========")
console.log("========= 调⽤开始录像失败 ===========")      }
})
},
/**
* 结束录像
*/
stopShootVideo() {
开车步骤wx.hideLoading();
// console.log("========= 调⽤结束录像 ===========")
compressed: true, //压缩视频
success: (res) => {
console.log(res)
this.setData({
videoSrc: pVideoPath
})
},
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;
欢乐谷 上海
//判断是点击还是长按点击不做任何事件,长按触发结束录像    if (endTime - this.data.startTime > 350) {
//长按操作调⽤结束录像⽅法
this.stopShootVideo();
} else {
this.setData({
textFlag: ''
})
}
},
/**
* 长按按钮进⾏录像
*/
handleLongPress: function (e) {
// 长按⽅法触发,调⽤开始录像⽅法
this.startShootVideo();
},
注:由于官⽅限制,只能录制30秒。
⽅法⼆
在js中
wx.chooseVideo({
sourceType: ['album','camera'],  maxDuration: 60,
camera: 'back',
success(res) {
console.pFilePath)  }
})

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。