volume.vue 7.6 KB

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