This commit is contained in:
yumoqing 2025-09-29 21:01:13 +08:00
parent b7a09e415c
commit 7f8b1903c2
2 changed files with 33 additions and 22 deletions

View File

@ -505,24 +505,25 @@ bricks.UiFile = class extends bricks.VBox {
if (this.opts.multiple){ if (this.opts.multiple){
files = []; files = [];
event.target.files.forEach(f => { event.target.files.forEach(f => {
files.push(f); if (! this.accept || f.type.startsWith(this.accept)){
files.push(f);
}
}); });
this.value = files; this.value = files;
return } else {
const file = event.target.files[0];
this.value = file;
} }
const file = event.target.files[0]; this.dispatch('changed', this.value);
this.value = file; console.log('"changed" fired', this.value);
this.dispatch('changed', this.value)
} }
dropHandle(event){ dropHandle(event){
event.preventDefault(); event.preventDefault();
this.set_css('hover', true);
var files = []; var files = [];
for (const f of event.dataTransfer.files) { for (const f of event.dataTransfer.files) {
if (this.opts.accept && ! f.type.startsWith(this.opts.accept)){ if (! this.opts.accept || f.type.startsWith(this.opts.accept)){
continue; files.push(f);
} }
files.push(f);
}; };
if (this.opts.multiple){ if (this.opts.multiple){
this.value = files; this.value = files;
@ -532,6 +533,7 @@ bricks.UiFile = class extends bricks.VBox {
this.set_input_file([this.value]); this.set_input_file([this.value]);
} }
this.dispatch('changed', this.value); this.dispatch('changed', this.value);
console.log('"changed" fired', this.value);
} }
set_formdata(fd){ set_formdata(fd){
fd.append(this.name, this.resultValue()); fd.append(this.name, this.resultValue());
@ -583,9 +585,9 @@ bricks.UiVideo =class extends bricks.UiFile {
} }
accept_video(recorder, event){ accept_video(recorder, event){
recorder.dismiss(); recorder.dismiss();
this.value = event.params this.value = event.params.file
console.log('record finished, value=', this.value); console.log('record finished, value=', this.value);
this.dispatch('changed', this.value); this.dispatch('changed', event.params.url);
} }
show_video(event){ show_video(event){
var params = event.params; var params = event.params;

View File

@ -34,6 +34,10 @@ bricks.MediaRecorder = class extends bricks.Popup {
this.toggle_record.disabled(true); this.toggle_record.disabled(true);
schedule_once(this.open_recorder.bind(this), 0.1); schedule_once(this.open_recorder.bind(this), 0.1);
} }
tick_task(){
this.task = schedule_once(this.tick_taask.bind(this), this.task_period);
this.timepass.set_text(bricks.timeDiff(this.start_time));
}
async switch_record(){ async switch_record(){
console.log('toggle_record called'); console.log('toggle_record called');
if (this.record_status == 'standby'){ if (this.record_status == 'standby'){
@ -71,16 +75,21 @@ bricks.MediaRecorder = class extends bricks.Popup {
const blob = new Blob(this.recordedChunks, const blob = new Blob(this.recordedChunks,
{ type: this.mimetype }); { type: this.mimetype });
// 1. 在本地播放 // 1. 在本地播放
const videoURL = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
// 2. 转成 File 对象 // 2. 转成 File 对象
const file = new File([blob], const file = new File([blob],
"recorded_video.webm", "recorded_video.webm",
{ type: this.mimetype }); { type: this.mimetype });
this.dispatch('record_end', file); var data = {
url: url,
file: file
}
this.dispatch('record_end', data);
console.log('"record_end" fired', file); console.log('"record_end" fired', file);
}; };
this.start_time = Date.now(); this.start_time = Date.now();
this.task = schedule_once(this.tick_taask.bind(this), this.task_period);
this.mediaRecorder.start(); this.mediaRecorder.start();
this.dispatch('record_started') this.dispatch('record_started')
console.log("Recording started..."); console.log("Recording started...");
@ -97,6 +106,10 @@ bricks.MediaRecorder = class extends bricks.Popup {
} }
close_recorder(){ close_recorder(){
if (this.task){
this.task.cancel();
this.task = null;
}
if (this.stream){ if (this.stream){
if (this.mediaRecorder){ if (this.mediaRecorder){
this.mediaRecorder.stop(); this.mediaRecorder.stop();
@ -148,9 +161,12 @@ bricks.SysVideoRecorder = class extends bricks.MediaRecorder {
this.camera_width = settings.width; this.camera_width = settings.width;
this.imgw = new bricks.Image({width: '100%'}); this.imgw = new bricks.Image({width: '100%'});
this.preview.add_widget(this.imgw); this.preview.add_widget(this.imgw);
this.task = schedule_once(this.show_picture.bind(this), this.task_period);
this.toggle_record.disabled(false); this.toggle_record.disabled(false);
} }
tick_task(){
super.tick_task();
this.show_picture();
}
async show_picture(){ async show_picture(){
if (this.task_period == 0){ if (this.task_period == 0){
return; return;
@ -161,13 +177,6 @@ bricks.SysVideoRecorder = class extends bricks.MediaRecorder {
this.task = schedule_once(this.show_picture.bind(this), this.task = schedule_once(this.show_picture.bind(this),
this.task_period); this.task_period);
} }
close_recorder(){
super.close_recorder();
if (this.task){
this.task.cancel();
this.task = null;
}
}
} }
bricks.SysCamera= class extends bricks.SysVideoRecorder { bricks.SysCamera= class extends bricks.SysVideoRecorder {
switch_record(){ switch_record(){
@ -180,7 +189,7 @@ bricks.SysCamera= class extends bricks.SysVideoRecorder {
this.task_period = 0; this.task_period = 0;
this.task = null; this.task = null;
this.dispatch('shot', this.dataurl); this.dispatch('shot', this.dataurl);
this.dismiss(); this.close_recorder();
} }
} }
bricks.Factory.register('SysCamera', bricks.SysCamera); bricks.Factory.register('SysCamera', bricks.SysCamera);