Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ const emit = defineEmits(['update:data', 'regeneration'])

const audioPlayer = ref<HTMLAudioElement[] | null>([])
const audioCiontainer = ref<HTMLDivElement>()
const audioPlayerStatus = ref(false)
const buttonData = ref(props.data)
const loading = ref(false)

Expand Down Expand Up @@ -271,13 +270,15 @@ class AudioManage {
tryList: Array<number>
ttsType: string
root: Element
is_end: boolean
constructor(ttsType: string, root: HTMLDivElement) {
this.textList = []
this.audioList = []
this.statusList = []
this.tryList = []
this.ttsType = ttsType
this.root = root
this.is_end = false
}
appendTextList(textList: Array<string>) {
const newTextList = textList.slice(this.textList.length)
Expand All @@ -300,8 +301,9 @@ class AudioManage {
audioElement.onended = () => {
this.statusList[index] = AudioStatus.END
// 如果所有的节点都播放结束
if (this.statusList.every((item) => item === AudioStatus.END)) {
if (this.statusList.every((item) => item === AudioStatus.END) && this.is_end) {
this.statusList = this.statusList.map((item) => AudioStatus.READY)
this.is_end = false
} else {
// next
this.play()
Expand All @@ -323,21 +325,18 @@ class AudioManage {
const text = await res.text()
MsgError(text)
this.statusList[index] = AudioStatus.ERROR
this.play()
return
throw ''
}
// 假设我们有一个 MP3 文件的字节数组
// 创建 Blob 对象
const blob = new Blob([res], { type: 'audio/mp3' })

// 创建对象 URL
const url = URL.createObjectURL(blob)
audioElement.src = url
this.statusList[index] = AudioStatus.READY
this.play()
})
.catch((err) => {
console.log('err: ', err)
this.statusList[index] = AudioStatus.ERROR
this.play()
})
Expand All @@ -348,9 +347,6 @@ class AudioManage {
const speechSynthesisUtterance: SpeechSynthesisUtterance = new SpeechSynthesisUtterance(
text
)
speechSynthesisUtterance.onpause = () => {
console.log('onpause')
}
speechSynthesisUtterance.onend = () => {
this.statusList[index] = AudioStatus.END
// 如果所有的节点都播放结束
Expand Down Expand Up @@ -389,8 +385,8 @@ class AudioManage {
if (res.type === 'application/json') {
const text = await res.text()
MsgError(text)
this.statusList[index] = AudioStatus.ERROR
return

throw ''
}
// 假设我们有一个 MP3 文件的字节数组
// 创建 Blob 对象
Expand All @@ -405,6 +401,7 @@ class AudioManage {
.catch((err) => {
console.log('err: ', err)
this.statusList[index] = AudioStatus.ERROR
this.play()
})
}
}
Expand All @@ -414,6 +411,9 @@ class AudioManage {
return this.statusList.some((item) => [AudioStatus.PLAY_INT].includes(item))
}
play(text?: string, is_end?: boolean, self?: boolean) {
if (is_end) {
this.is_end = true
}
if (self) {
this.tryList = this.tryList.map((item) => 0)
}
Expand All @@ -431,7 +431,6 @@ class AudioManage {
const index = this.statusList.findIndex((status) =>
[AudioStatus.MOUNTED, AudioStatus.READY].includes(status)
)

if (index < 0 || this.statusList[index] === AudioStatus.MOUNTED) {
return
}
Expand Down Expand Up @@ -497,6 +496,7 @@ class AudioManage {
},
is_end
)

return split
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a comprehensive check of the code with suggestions:

  1. Refactoring audioPlayerStatus: Removed because it wasn't being used.

  2. Optimization in appendTextList:

    • Use functional syntax to slice arrays.
    • Remove unused variables (newTextList, _nextIndex).
    appendTextList(textList: Array<string>): void {
      this.textList.push(...textList);
    }
  3. Simplify AudioManage Constructor:

    • Move default values directly into the constructor parameters.
    class AudioManage {
      textList = [];
      audioList = [];
      statusList = [];
      tryList = [];
      ttsType;
      root: HTMLDivElement;
  • isEnd = false;

    constructor(public ttsType: string, public root: HTMLDivElement) {}
    }


4. **Optimize `fetchAndPlay` for Speech Synthesis**:
- Simplify error handling and ensure errors are caught properly.

```javascript
async fetchAndPlay(url: string): Promise<void> {
  try {
    const response = await fetch(url);

    if (!response.ok) {
      const errorMessage = await response.text();
      MsgError(errorMessage);
      this.statusList[index] = AudioStatus.ERROR;
      throw new Error(errorMessage);
    }

    const blob = new Blob([response.arrayBuffer()], { type: 'audio/mpeg' });
    const url = URL.createObjectURL(blob);

    const html = `
      <div style="height: 80px; background-color: #e0f7fa;">
        <iframe width="100%" height="80" src="${url}" frameborder="0"></iframe>
      </div>
    `;
    $(element).html(html);
    return `<source id="player-${key}-${index}" src="" ${style}></source>`;
  } catch (error) {
    console.error('Failed to load media:', error);
    this.statusList[this.activeIndex] = AudioStatus.ERROR;
    this.play(nextSource ? nextSource : this.nextSrcs[0]);
  }
}
  1. Minor Code Style Enhancements:
    • Ensure consistent indentation and spacing around logical operators.

These optimizations improve readability and maintainability while minimizing unnecessary complexity.

Expand Down