完整的代码如下:
wxml
<view class="container">
<camera device-position="back" flash="off" class="camera"></camera>
<view class="btngroup">
<button bindtap="startRecording">开始录制</button>
<button bindtap="stopRecording">停止录制</button>
</view>
<video wx:if="{{videoSrc}}" src="{{videoSrc}}" controls></video>
</view>jsPage({
data: {
videoSrc: '',
isRecording: false
},
onLoad: function() {
},
startRecording: function() {
const that = this;
const cameraContext = wx.createCameraContext();
cameraContext.startRecord({
success: (res) => {
console.log('开始录制视频');
that.setData({
isRecording: true
});
},
fail: (error) => {
console.error('开始录制失败:', error);
wx.showToast({
title: '开始录制失败',
icon: 'none'
});
}
});
},
stopRecording: function() {
const that = this;
const cameraContext = wx.createCameraContext();
cameraContext.stopRecord({
success: (res) => {
console.log('录制视频成功');
that.setData({
isRecording: false,
videoSrc: res.tempVideoPath
});
that.saveVideo(res.tempVideoPath);
},
fail: (error) => {
console.error('停止录制失败:', error);
wx.showToast({
title: '停止录制失败',
icon: 'none'
});
}
});
},
saveVideo: function(videoPath) {
const that = this;
wx.showLoading({
title: '正在处理视频',
});
wx.saveVideoToPhotosAlbum({
filePath: videoPath,
success: function(res) {
wx.hideLoading();
wx.showToast({
title: '视频已保存',
icon: 'success'
});
},
fail: function(error) {
wx.hideLoading();
console.error('保存视频失败:', error);
wx.showToast({
title: '保存视频失败',
icon: 'none'
});
}
});
}
});wxss.container {
position: relative;
width: 100%;
height: 100vh;
}
.camera {
width: 100%;
height: 100%;
}
.btngroup {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
} 网友回复
webgl与webgpu有啥不同?
Zero Trust的Tunnels怎么设置泛域名解析及http服务获取当前访问域名?
Spec Coding(规范驱动编码)和 Vibe Coding(氛围编程)有啥区别?
如何在国内服务器上正常运行未备案的域名网站?
Cloudflared 和WARP Connector有啥不同?
有没有让本地开源大模型越狱的方法或插件啥的?
如何使用Zero Trust的Tunnels技术将局域网电脑web服务可以公网访问呢?
编程领域ai大模型的排名是怎么样的?
如何修改别人发给我的微信笔记内容?
fbx、obj、glb三维格式模型如何在浏览器中通过three相互转换格式?


