完整的代码如下:
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%);
} 网友回复
如何破解绕开seedance2.0真人照片生成视频 限制?
python有哪些算法可以将视频中的每个帧图片去除指定区域水印合成新的视频?
iphone的激光雷达数据能否实时传输到three三维空间中?
豆包sora等ai视频生成大模型生成的视频水印如何去除?
python如何实现在电脑上拨号打电话给手机?
具身机器人与人形机器人区别?
nodejs如何将一个完整的js代码文件切割成不同的部分混淆后动态加载进入html运行?
为啥windows.onerror捕获js错误是这样的{"message":"Script error.","source":"","lineno":0,"colno":0,"stack":null,
2026年ai将全面接管编程?
WebMCP是干啥的?


