Commit 2e01e315 authored by JinWei's avatar JinWei

feat: 新增文本转语音

parent cbb605d2
This diff is collapsed.
...@@ -27,9 +27,10 @@ ...@@ -27,9 +27,10 @@
"@tweenjs/tween.js": "~18.5.0", "@tweenjs/tween.js": "~18.5.0",
"ali-oss": "^6.5.1", "ali-oss": "^6.5.1",
"compressing": "^1.5.0", "compressing": "^1.5.0",
"crypto-js": "^4.2.0",
"ng-zorro-antd": "^8.5.2", "ng-zorro-antd": "^8.5.2",
"querystring": "^0.2.0",
"rxjs": "~6.5.4", "rxjs": "~6.5.4",
"node-sass": "4.0.0",
"spark-md5": "^3.0.0", "spark-md5": "^3.0.0",
"tslib": "^1.10.0", "tslib": "^1.10.0",
"zone.js": "~0.10.2" "zone.js": "~0.10.2"
...@@ -39,6 +40,7 @@ ...@@ -39,6 +40,7 @@
"@angular/cli": "~9.0.3", "@angular/cli": "~9.0.3",
"@angular/compiler-cli": "~9.0.2", "@angular/compiler-cli": "~9.0.2",
"@angular/language-service": "~9.0.2", "@angular/language-service": "~9.0.2",
"@types/crypto-js": "^4.2.2",
"@types/jasmine": "~3.5.0", "@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3", "@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1", "@types/node": "^12.11.1",
......
...@@ -5,7 +5,7 @@ import {HttpClient, HttpHeaders, HttpRequest, HttpEvent, HttpEventType, HttpResp ...@@ -5,7 +5,7 @@ import {HttpClient, HttpHeaders, HttpRequest, HttpEvent, HttpEventType, HttpResp
import {NzMessageService, UploadChangeParam, UploadFile, UploadFileStatus, UploadXHRArgs} from 'ng-zorro-antd'; import {NzMessageService, UploadChangeParam, UploadFile, UploadFileStatus, UploadXHRArgs} from 'ng-zorro-antd';
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser'; import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import cryptoJs from 'crypto-js'; import * as cryptoJs from 'crypto-js';
import {v4} from 'uuid'; import {v4} from 'uuid';
function httpHeadCall(requsetUrl, callback) { function httpHeadCall(requsetUrl, callback) {
......
...@@ -155,7 +155,14 @@ ...@@ -155,7 +155,14 @@
<div style="margin-top: 5px; display: flex; align-items: center; "> <div style="margin-top: 5px; display: flex; align-items: center; ">
<span style="">文本:</span> <span style="">文本:</span>
<input type="text" nz-input [(ngModel)]="option.text" (blur)="save()" style=" width: 300px;"> <input type="text" nz-input [(ngModel)]="option.text" (blur)="save()" style=" width: 300px; margin-right: 8px;">
<app-audio-recorder [audioUrl]="option.audio_url"
(audioUploaded)="onItemAudioUploadSuccess($event, option, 'audio_url')">
</app-audio-recorder>
<button nz-button nzType="primary" (click)="generateTTSHandle(option, option.text, 'audio_url')" style="width: 120px; height: 32px; margin-left: 8px;">
<i nz-icon nzType="plus-circle" nzTheme="outline"></i>
生成音频
</button>
</div> </div>
<!-- <div style="margin-top: 5px; display: flex; align-items: center; "> <!-- <div style="margin-top: 5px; display: flex; align-items: center; ">
<span style="">翻译:</span> <span style="">翻译:</span>
...@@ -175,7 +182,7 @@ ...@@ -175,7 +182,7 @@
</button> </button>
</div> </div>
<div *ngIf="ques.optionTypeAudio" style="margin-top: 10px; display: flex; align-items: center;"> <div *ngIf="ques.optionTypeAudio" style="display: none;">
<span style="">音频:</span> <span style="">音频:</span>
<app-audio-recorder [audioUrl]="option.audio_url" <app-audio-recorder [audioUrl]="option.audio_url"
(audioUploaded)="onItemAudioUploadSuccess($event, option, 'audio_url')"> (audioUploaded)="onItemAudioUploadSuccess($event, option, 'audio_url')">
......
...@@ -54,10 +54,51 @@ export class FormComponent implements OnInit, OnChanges, OnDestroy { ...@@ -54,10 +54,51 @@ export class FormComponent implements OnInit, OnChanges, OnDestroy {
selectedWord: any; selectedWord: any;
constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef, constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef,
private message: NzMessageService, private http: HttpClient) { private message: NzMessageService, private http: HttpClient, private nzMessageService: NzMessageService) {
} }
async generateTTSHandle(obj, text, key = 'audio') {
if (!text) {
this.nzMessageService.error('文本不能为空');
return;
}
this.nzMessageService.loading('生成音频中...');
const audioUrl = await this.generateTTS(text).catch((e) => {
this.nzMessageService.error('生成音频失败');
}).finally(() => {
this.nzMessageService.remove();
});
if (!audioUrl) {
this.nzMessageService.error('生成音频失败,请稍后再试。');
return;
}
obj[key] = audioUrl;
this.save();
}
async generateTTS(sentence: string): Promise<string> {
const commonPostMessageWithCallback = (window as any).commonPostMessageWithCallback;
if (typeof commonPostMessageWithCallback !== 'function') {
throw new Error('通信方法不可用');
}
return new Promise<string>((resolve, reject) => {
commonPostMessageWithCallback(
{ action: 'generateTTS', data: sentence },
(res: string | object) => {
const resData = typeof res === 'string' ? JSON.parse(res) : res;
if (resData.msg == 'success' && resData.data) {
resolve(resData.data as unknown as string);
} else {
reject('音频生成失败');
}
}
);
}).catch(() => {
throw new Error('音频生成失败');
});
}
ngOnInit() { ngOnInit() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment