WXML页面放置一个camera组件。
<cover-view class="container">
<cover-view class="takePhoto" bindtap="startTakePhoto">
<cover-image class="img" src="网络或本地都可以">
</cover-image></cover-view>
<cover-view class="time">00:00</cover-view>
</cover-view>
</camera>
并且在组件内放置cover-view
cover-image
组件,用来控制前后摄像头切换、开始和暂停录制、录制进度。
接下来是js页面:
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function (res) {
if (wx.createCameraContext()) {
this.cameraContext = wx.createCameraContext('myCamera')
} else {
// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
}
},
startTakePhoto: function () {
this.cameraContext.startRecord({
success: function (res) {
console.log('成功!')
console.log(res)
},
fail: function (res) {
console.log('失败!')
console.log(res)
},
complete: function (res) {
console.log('complete!')
console.log(res)
},
timeoutCallback: function (res) {
console.log('超时')
console.log(res)
}
})
},
wxss文件
.camera{
height:1080rpx;
width: 750rpx;
}
.pause,.time {
flex: 1;
height: 100%;
}
.time {
text-align: center;
background-color: rgba(0, 0, 0, .5);
color: white;
line-height: 50px;
}
.img {
width: 40px;
height: 40px;
margin: 5px auto;
}