electron 在回调函数中调用注入的服务

h43kikqp  于 2022-12-08  发布在  Electron
关注(0)|答案(1)|浏览(205)

我正在开发电子/Angular 13应用程序
我有以下功能:

async handleStop(e: any) {
    const blob = new Blob(this.recordChunks, {
        type: 'video/webm; codecs=vp9'
    });
    
    const buffer = Buffer.from(await blob.arrayBuffer());
    console.log(buffer)

    this._electronService.ipcRenderer.send('stop-recording', buffer);
}

我将其用作回调函数,如下所示:

async onSourceSelected(displayId: string) {
    let constraints = {
        audio: false,
        video: {
            mandatory: {
                chromeMediaSource: 'desktop',
                chromeMediaSourceId: displayId
            }
        }
    };

    // CREATE A STREAM
    let stream = await (navigator.mediaDevices as  any).getUserMedia(constraints);
    
    // Create the media Recorder
    const options = { mimeType: 'video/webm; codecs=vp9' };
    this.mediaRecorder = new MediaRecorder(stream, options);
    
    // Register event handler
    this.mediaRecorder.ondataavailable = this.handleDataAvailable;
    this.mediaRecorder.onstop = this.handleStop;
};

组件的构造函数:

constructor(private dialogRef: DialogRef, @Inject(DIALOG_DATA) public data: any, private _electronService: ElectronApiServiceService, private zone: NgZone, private dialog: DialogService) { }

“_electronService”是一个注入到组件构造函数中的Angular服务。当调用“handleStop”方法时,我在控制台中得到以下错误:
“未处理的承诺拒绝:无法读取未定义的属性(阅读“ipcRenderer”);区域:;任务:答应吧。值:类型错误:无法读取未定义的属性(阅读'ipcRenderer')”

uyhoqukh

uyhoqukh1#

需要显式地将事件处理程序绑定到this。当您将handleStop赋值给onstop时,它会丢失上下文,因此handleStop函数中的this不会引用对象本身。

this.mediaRecorder.onstop = this.handleStop.bind(this);

相关问题