Spaces:
Sleeping
Sleeping
const plugin = requirePlugin('WechatSI'); | |
Page({ | |
data: { | |
languages: { | |
'zh': { name: '中文', flag: 'cn', code: 'zh_CN' }, | |
'en': { name: 'English', flag: 'us', code: 'en_US' } | |
}, | |
langCodes: ['zh', 'en'], // Only CN and EN for this test version | |
sourceLang: 'zh', | |
targetLang: 'en', | |
transcript: '', | |
outputText: '', | |
isRecording: false, | |
sourceLanguages: [], | |
targetLanguages: [] | |
}, | |
onLoad: function () { | |
this.initializeLanguages(); | |
this.initRecorderManager(); | |
}, | |
// --- Language Selection Logic (Simplified for CN/EN only) --- | |
initializeLanguages: function () { | |
const { langCodes, languages, sourceLang, targetLang } = this.data; | |
this.setData({ | |
sourceLanguages: langCodes.map(c => ({ ...languages[c], langCode: c, selected: c === sourceLang })), | |
targetLanguages: langCodes.map(c => ({ ...languages[c], langCode: c, selected: c === targetLang })) | |
}); | |
}, | |
selectSourceLanguage: function(e) { this.setData({ sourceLang: e.currentTarget.dataset.langCode }, this.initializeLanguages); }, | |
selectTargetLanguage: function(e) { this.setData({ targetLang: e.currentTarget.dataset.langCode }, this.initializeLanguages); }, | |
swapLanguages: function() { | |
this.setData({ | |
sourceLang: this.data.targetLang, | |
targetLang: this.data.sourceLang, | |
transcript: this.data.outputText, | |
outputText: this.data.transcript | |
}, this.initializeLanguages); | |
}, | |
// --- Recorder Manager Initialization (Pure Plugin Mode) --- | |
initRecorderManager: function() { | |
const manager = plugin.getRecordRecognitionManager(); | |
manager.onStart = () => { | |
this.setData({ transcript: '正在聆听...', outputText: '' }); | |
}; | |
// Only onStop is used for final results | |
manager.onStop = (res) => { | |
this.setData({ isRecording: false }); | |
if (res.result) { | |
this.setData({ transcript: res.result, outputText: res.translateResult || '' }); | |
} else { | |
this.setData({ transcript: '识别结果为空', outputText: '' }); | |
} | |
}; | |
manager.onError = (res) => { | |
this.setData({ isRecording: false, transcript: '识别失败', outputText: res.msg }); | |
}; | |
// CRITICAL: Save the manager instance to the page context | |
this.manager = manager; | |
}, | |
// --- Recording Start/Stop (Pure Plugin Mode) --- | |
startRecording: function () { | |
const { sourceLang, targetLang } = this.data; | |
this.setData({ isRecording: true }); | |
this.manager.start({ lang: this.data.languages[sourceLang].code, translate: true, lto: this.data.languages[targetLang].code }); | |
}, | |
stopRecording: function () { | |
this.manager.stop(); | |
} | |
}); |