完整的代码如下:
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>js
Page({ 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%); }
网友回复
浏览器中如何将WebM视频转成mp4视频?
parlant如何改成qwen 的apikey与baseurl?
如何写一个chrome插件实现截屏自动生成步骤图文教程转成pdf或网页?
python如何通过阿里云的api对域名进行解析和ecs主机服务器进行启动停止等操作?
Tesla Robotaxi可以让特斯拉车自动无人驾驶跑滴滴为车主赚钱,国内以后也会这样吗?
有没有可以监控安卓手机上的app打开后偷偷摸摸做了啥的监控软件?
webrtc进行p2p连接发送的文本音视频文件是否是加密的?
如何让一个可爱的三维动物通过three在浏览器中有表情动作的自然说话?
go与wails如何开发一个高性能的原生桌面应用?
python如何调用openai的api实现知识讲解类动画讲解视频的合成?