办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<script setup lang="ts">
import { mlog } from '@/api'; 
import { ref } from "vue";
import { NButton,NInput } from "naive-ui";
 
const f = ref({text:'Hi,google ! I am a good student!'});
const go = async () => {
    
  const apiKey = 'sdsd-121212';
 
const apiUrl = 'https://api.openai-sk.com/v1/audio/speech';
const ttsModel = 'tts-1';
const voice = 'alloy';
//const inputText = 'I am a good student!';
 
//const fetchData = async () => {
  try {
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`,
      },
      body: JSON.stringify({
        model: ttsModel,
        input: f.value.text ,
        voice: voice,
      }),
    });
 
    if (!response.ok) {
      throw new Error(`API request failed with status ${response.status}`);
    }
 
    const audioData = await response.arrayBuffer();
    const blob = new Blob([audioData], { type: 'audio/mp3' });
    mlog('blob', blob);
 
    const player = new window.Audio(); 
    player.src = URL.createObjectURL(blob);
    player.addEventListener('ended', () => {
      mlog('音乐播放完毕');
    });
     player.addEventListener('loadedmetadata', () => {
      mlog('时长', player.duration);
    });
    player.load(); 
    player.play();
 
 
     const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = 'speech.mp3';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  }catch (error) {
    console.error('Error:', error);
  }
 
 
}
//go();
 
  const ccgo= (event:any )=> {
    var file = event.target.files[0];
 
    // 通过 FileReader 读取文件内容并创建 Blob 对象
    var reader = new FileReader();
    reader.onload = function(e:any ) {
      var blob = new Blob([e.target.result], { type: 'audio/mp3' });
      mlog('blob', blob);
 
      // 创建 Howl 实例
    //  let  sound = new Howl({
    //     src: [blob],
    //     format: ['mp3'],
    //     volume: 0.5,
    //     onend: function() {
    //       console.log('音乐播放完毕');
    //     }
    //     ,onloaderror:(e:any )=>{
    //          mlog('onloaderror' ,e  )
    //     }
    //   });
    //   sound.play(); 
    const player = new window.Audio(); 
    player.src = URL.createObjectURL(blob);
    player.addEventListener('ended', () => {
      mlog('音乐播放完毕');
    });
     player.addEventListener('loadedmetadata', () => {
      mlog('时长', player.duration);
    });
    player.load(); 
    player.play();
 
    };
 
    reader.readAsArrayBuffer(file);
  } 
</script>
<template>
<!-- <div class="text-red-300" >good</div>
<div class="text-red-300" @click="go" >go</div>
<div class="text-red-300" @click="sound.stop() " >eend</div> -->
<div class="p-4 space-y-4">
<NInput v-model:value="f.text" type="textarea"></NInput>
<NButton @click="go" type="primary">提交</NButton>
<div>
<input type="file" id="audioFile" accept="audio/*" @change="ccgo">
</div>
</div>
 
 
</template>