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
| <template>
| <view>
| <input v-model="text" placeholder="请输入要生成语音的文字" class="input_style" />
| <view @click="requestAPI" class="btn">生成语音</view>
|
| <view v-if="url && status==='no'" class="btn" @click="playVoice">播放语音</view>
| <view v-else-if="status==='loading'" style="margin-top: 50rpx;">
| <image src="/static/icon_spinner.png" style="width: 68rpx;height: 68rpx;" class="rotating-element"></image>
| </view>
| </view>
| </template>
|
| <script>
| // 语音播放器实例
| let mp3 = uni.createInnerAudioContext();
|
| export default {
| data() {
| return {
| text:'',
| status: 'none',
| url: ''
| }
| },
| methods: {
| playVoice(){
| mp3.src = this.url;
| mp3.play();
| },
| requestAPI(){
| if(!this.text || this.text.length < 1){
| uni.showToast({
| title:'请输入内容',
| icon:'none'
| })
| return;
| }
|
| let that = this;
| that.status = 'loading';
|
| uni.request({
| method:'POST',
| url:'https://5555api.com/data/api/fetchVoiceByText',
| data:{
| apikey: 'test_app_key_5555api.com',
| text: that.text,
| },
| success(res) {
| that.status = 'no';
| console.error('请求成功:', res);
| that.url = res.data.data.data;
| },
| fail() {
| that.status = 'no';
| }
| });
| }
| }
| }
| </script>
|
| <style>
| .input_style{
| width: 600rpx;
| margin-left: 74rpx;
| margin-right: 75rpx;
| border: 2rpx solid #12c212;
| padding: 10rpx 20rpx;
| border-radius: 10rpx;
| font-size: 28rpx;
| margin-top: 50rpx;
| }
|
| .btn{
| background-color: #12c212;
| color: #ffffff;
| display: flex;
| width: 450rpx;
| margin-left: 150rpx;
| margin-right: 149rpx;
| flex-direction: column;
| align-items: center;
| justify-content: center;
| font-size: 30rpx;
| padding: 20rpx 0rpx;
| margin-top: 50rpx;
| }
| </style>
|
|