volume.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view class="settings-container">
  3. <!-- 设置选项组 -->
  4. <view class="settings-section">
  5. <u-cell-group :border="false">
  6. <!-- 语音播报设置 -->
  7. <u-cell title="语音播报" label="控制除app启动提示外所有语音播报的开关" :border-bottom="true">
  8. <template #right-icon>
  9. <u-switch v-model="settings.audioPlay" activeColor="#4cd964"></u-switch>
  10. </template>
  11. </u-cell>
  12. <!-- 点击音效设置 -->
  13. <u-cell title="点击音效" :border-bottom="true">
  14. <template #right-icon>
  15. <u-switch v-model="settings.audioClick" activeColor="#4cd964"></u-switch>
  16. </template>
  17. </u-cell>
  18. <!-- 启动提示语设置 -->
  19. <u-cell title="启动提示语" :border-bottom="true">
  20. <template #right-icon>
  21. <u-switch v-model="settings.audioStartSwitch" activeColor="#4cd964"></u-switch>
  22. </template>
  23. </u-cell>
  24. </u-cell-group>
  25. <!-- 启动提示语输入框 -->
  26. <view class="input-section">
  27. <u-textarea v-model="settings.audioStart" placeholder="输入启动时要说的内容,比如:欢迎使用书嗨/很开心又见到你啦"
  28. height="100"></u-textarea>
  29. </view>
  30. <!-- 语速设置 -->
  31. <view class="slider-section">
  32. <view class="slider-title">
  33. <text>设置语速</text>
  34. <text class="subtitle">(部分机型不支持)</text>
  35. </view>
  36. <u-slider :value="settings.audioSpeed" :max="20" :min="5" :step="1" showValue @change="changeSpeed"></u-slider>
  37. </view>
  38. <!-- 音调设置 -->
  39. <view class="slider-section">
  40. <view class="slider-title">
  41. <text>设置音调</text>
  42. <text class="subtitle">(部分机型不支持)</text>
  43. </view>
  44. <u-slider :value="settings.audioVolume" :max="10" :min="1" :step="1" showValue @change="changeVolume"></u-slider>
  45. </view>
  46. <!-- 操作按钮 -->
  47. <view class="button-group">
  48. <u-button type="primary" text="保存设置并试听" @click="saveVoiceBroadcast"></u-button>
  49. <u-button type="warning" text="停止播放" @click="stopPlayback" :plain="true"></u-button>
  50. <u-button type="error" text="重启app" @click="restartApp" :plain="true"></u-button>
  51. </view>
  52. </view>
  53. </view>
  54. </template>
  55. <script setup>
  56. import {
  57. ref,
  58. reactive,
  59. onMounted
  60. } from 'vue'
  61. import VolumeTTS from '@/utils/VolumeTTS';
  62. // 设置状态
  63. const settings = ref({
  64. audioStartSwitch: true,
  65. audioStart: '书嗨,不辜负每一个爱书的人',
  66. audioSpeed: 10,
  67. audioVolume: 5,
  68. "audioPlay": false,
  69. "audioClick": false,
  70. })
  71. function changeSpeed(value) {
  72. settings.value.audioSpeed = value
  73. }
  74. function changeVolume(value) {
  75. settings.value.audioVolume = value
  76. }
  77. // 获取语音播报内容
  78. const getVoiceBroadcast = () => {
  79. uni.$u.http.get('/app/appConf/getAudioConfig').then(res => {
  80. if (res.code == 200) {
  81. settings.value = res.data
  82. }
  83. })
  84. }
  85. getVoiceBroadcast()
  86. // 保存语音播报内容
  87. const saveVoiceBroadcast = () => {
  88. uni.$u.http.post('/app/appConf/setAudioConfig', settings.value).then(res => {
  89. if (res.code == 200) {
  90. uni.showToast({
  91. title: '设置成功',
  92. icon: 'success'
  93. })
  94. initTTS()
  95. }
  96. })
  97. }
  98. //初始化tts
  99. const tts = ref(null)
  100. const initTTS = () => {
  101. let params = {
  102. speed: settings.value.audioSpeed / 10, // 将 0-10 转换为 0-1
  103. pitch: settings.value.audioVolume, // 将 0-10 转换为 0-1
  104. volume: 1,
  105. }
  106. tts.value = new VolumeTTS(params)
  107. settings.value.audioStartSwitch && tts.value.speak(settings.value.audioStart)
  108. }
  109. // 停止播放
  110. function stopPlayback() {
  111. tts.value && tts.value.stop()
  112. uni.showToast({
  113. title: '已停止播放',
  114. icon: 'none'
  115. })
  116. }
  117. // 重启应用
  118. function restartApp() {
  119. uni.showModal({
  120. title: '提示',
  121. content: '确定要重启应用吗?',
  122. success: function (res) {
  123. if (res.confirm) {
  124. // #ifdef APP-PLUS
  125. plus.runtime.restart()
  126. // #endif
  127. // #ifdef H5 || MP-WEIXIN
  128. uni.reLaunch({
  129. url: '/pages/index/index'
  130. })
  131. // #endif
  132. }
  133. }
  134. })
  135. }
  136. // 加载保存的设置
  137. function loadSettings() {
  138. try {
  139. const savedSettings = uni.getStorageSync('tts_settings')
  140. if (savedSettings) {
  141. Object.assign(settings, savedSettings)
  142. }
  143. } catch (error) {
  144. console.error('加载设置失败:', error)
  145. }
  146. }
  147. // 播放点击音效
  148. function playClickSound() {
  149. if (!settings.audioClick) return
  150. const audio = uni.createInnerAudioContext()
  151. audio.src = '/static/audio/click.mp3' // 确保添加点击音效文件
  152. audio.play()
  153. }
  154. // 生命周期钩子
  155. onMounted(async () => {
  156. loadSettings()
  157. })
  158. </script>
  159. <style lang="scss" scoped>
  160. .settings-container {
  161. min-height: 100vh;
  162. background-color: #f5f5f5;
  163. padding-bottom: 40rpx;
  164. .card-head {
  165. display: flex;
  166. align-items: center;
  167. padding: 10rpx 0;
  168. .head-title {
  169. margin-left: 10rpx;
  170. font-size: 32rpx;
  171. font-weight: bold;
  172. }
  173. }
  174. .settings-section {
  175. margin: 20rpx;
  176. background-color: #ffffff;
  177. border-radius: 12rpx;
  178. padding: 20rpx;
  179. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  180. }
  181. .input-section {
  182. margin: 30rpx 0;
  183. padding: 0 20rpx;
  184. }
  185. .slider-section {
  186. margin: 30rpx 0;
  187. padding: 0 20rpx;
  188. .slider-title {
  189. margin-bottom: 20rpx;
  190. font-size: 28rpx;
  191. color: #333;
  192. .subtitle {
  193. font-size: 24rpx;
  194. color: #999;
  195. margin-left: 10rpx;
  196. }
  197. }
  198. }
  199. .button-group {
  200. margin-top: 50rpx;
  201. padding: 0 20rpx;
  202. :deep(.u-button) {
  203. margin-bottom: 20rpx;
  204. }
  205. }
  206. }
  207. </style>