Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Support audio recording on HarmonyOS https://github.com/siyuan-note/siyuan/issues/18426

Daniele408ce8fe65a
diff --git a/app/src/protyle/breadcrumb/index.ts b/app/src/protyle/breadcrumb/index.ts
index cc5ce2fdb3..0526baaa76 100644
--- a/app/src/protyle/breadcrumb/index.ts
+++ b/app/src/protyle/breadcrumb/index.ts
@@ -672,62 +672,60 @@ ${padHTML}
window.siyuan.menus.menu.remove();
});
window.siyuan.menus.menu.append(uploadMenu);
- if (!isInHarmony()) {
- window.siyuan.menus.menu.append(new MenuItem({
- id: this.mediaRecorder?.isRecording ? "endRecord" : "startRecord",
- current: this.mediaRecorder && this.mediaRecorder.isRecording,
- icon: "iconRecord",
- label: this.mediaRecorder?.isRecording ? window.siyuan.languages.endRecord : window.siyuan.languages.startRecord,
- click: async () => {
- if (this.startingRecord || this.stoppingRecord) {
- return;
- }
- if (this.mediaRecorder?.isRecording) {
- this.stopRecord(protyle);
- return;
- }
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: this.mediaRecorder?.isRecording ? "endRecord" : "startRecord",
+ current: this.mediaRecorder && this.mediaRecorder.isRecording,
+ icon: "iconRecord",
+ label: this.mediaRecorder?.isRecording ? window.siyuan.languages.endRecord : window.siyuan.languages.startRecord,
+ click: async () => {
+ if (this.startingRecord || this.stoppingRecord) {
+ return;
+ }
+ if (this.mediaRecorder?.isRecording) {
+ this.stopRecord(protyle);
+ return;
+ }
- this.startingRecord = true;
- let mediaStream: MediaStream;
- try {
- /// #if !BROWSER
- if (window.siyuan.config.system.os === "darwin") {
- const status = await ipcRenderer.invoke(Constants.SIYUAN_GET, {cmd: "getMicrophone"});
- if (["denied", "restricted", "unknown"].includes(status)) {
- showMessage(window.siyuan.languages.microphoneDenied);
+ this.startingRecord = true;
+ let mediaStream: MediaStream;
+ try {
+ /// #if !BROWSER
+ if (window.siyuan.config.system.os === "darwin") {
+ const status = await ipcRenderer.invoke(Constants.SIYUAN_GET, {cmd: "getMicrophone"});
+ if (["denied", "restricted", "unknown"].includes(status)) {
+ showMessage(window.siyuan.languages.microphoneDenied);
+ return;
+ } else if (status === "not-determined") {
+ const isAccess = await ipcRenderer.invoke(Constants.SIYUAN_GET, {cmd: "askMicrophone"});
+ if (!isAccess) {
+ showMessage(window.siyuan.languages.microphoneNotAccess);
return;
- } else if (status === "not-determined") {
- const isAccess = await ipcRenderer.invoke(Constants.SIYUAN_GET, {cmd: "askMicrophone"});
- if (!isAccess) {
- showMessage(window.siyuan.languages.microphoneNotAccess);
- return;
- }
}
}
- /// #endif
+ }
+ /// #endif
- mediaStream = await navigator.mediaDevices.getUserMedia({
- audio: isInAndroid() ? {
- autoGainControl: false,
- echoCancellation: false,
- noiseSuppression: false,
- } : true,
- });
- await this.startRecord(protyle, mediaStream);
- } catch (error) {
- mediaStream?.getTracks().forEach((track) => track.stop());
- if (error instanceof RecordMediaInputEndedError) {
- showMessage(window.siyuan.languages.recordInterrupted);
- } else if (!isInAndroid() ||
- !(error instanceof DOMException && error.name === "NotAllowedError")) {
- showMessage(window.siyuan.languages["record-tip"]);
- }
- } finally {
- this.startingRecord = false;
+ mediaStream = await navigator.mediaDevices.getUserMedia({
+ audio: isInAndroid() || isInHarmony() ? {
+ autoGainControl: false,
+ echoCancellation: false,
+ noiseSuppression: false,
+ } : true,
+ });
+ await this.startRecord(protyle, mediaStream);
+ } catch (error) {
+ mediaStream?.getTracks().forEach((track) => track.stop());
+ if (error instanceof RecordMediaInputEndedError) {
+ showMessage(window.siyuan.languages.recordInterrupted);
+ } else if ((!isInAndroid() && !isInHarmony()) ||
+ !(error instanceof DOMException && error.name === "NotAllowedError")) {
+ showMessage(window.siyuan.languages["record-tip"]);
}
+ } finally {
+ this.startingRecord = false;
}
- }).element);
- }
+ }
+ }).element);
}
if (!protyle.disabled) {
window.siyuan.menus.menu.append(new MenuItem({
diff --git a/app/src/protyle/util/RecordMedia.ts b/app/src/protyle/util/RecordMedia.ts
index 2fe9ceffd6..4a59c7204a 100644
--- a/app/src/protyle/util/RecordMedia.ts
+++ b/app/src/protyle/util/RecordMedia.ts
@@ -1,4 +1,4 @@
-import {isInAndroid} from "./compatibility";
+import {isInAndroid, isInHarmony} from "./compatibility";
const MP3_BIT_RATE = 96;
const BUFFER_SIZE = 2048;
@@ -96,7 +96,7 @@ export class RecordMedia {
}
const contextOptions = {} as AudioContextOptions & { sinkId?: { type: "none" } };
- if (isInAndroid() && "setSinkId" in AudioContextConstructor.prototype) {
+ if ((isInAndroid() || isInHarmony()) && "setSinkId" in AudioContextConstructor.prototype) {
contextOptions.sinkId = {type: "none"};
}
this.context = new AudioContextConstructor(contextOptions);