volume.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, onUnmounted } from "vue";
  101. import VolumeTTS from "@/utils/VolumeTTS";
  102. // 设置状态
  103. const settings = ref({
  104. audioStartSwitch: true,
  105. audioStart: "书嗨,不辜负每一个爱书的人",
  106. audioSpeed: 10,
  107. audioVolume: 5,
  108. audioPlay: false,
  109. audioClick: false,
  110. });
  111. function changeSpeed(value) {
  112. settings.value.audioSpeed = value;
  113. }
  114. function changeVolume(value) {
  115. settings.value.audioVolume = value;
  116. }
  117. // 获取语音播报内容
  118. const getVoiceBroadcast = () => {
  119. uni.$u.http.get("/app/appConf/getAudioConfig").then((res) => {
  120. if (res.code == 200) {
  121. settings.value = res.data;
  122. }
  123. });
  124. };
  125. getVoiceBroadcast();
  126. // 保存语音播报内容
  127. const saveVoiceBroadcast = () => {
  128. uni.$u.http
  129. .post("/app/appConf/setAudioConfig", settings.value)
  130. .then((res) => {
  131. if (res.code == 200) {
  132. uni.showToast({
  133. title: "设置成功",
  134. icon: "success",
  135. });
  136. initTTS();
  137. let params = {
  138. speed: settings.value.audioSpeed / 10, // 将 0-10 转换为 0-1
  139. pitch: settings.value.audioVolume, // 将 0-10 转换为 0-1
  140. volume: 1,
  141. };
  142. uni.$u.ttsModule.setCustomOpts(params);
  143. }
  144. });
  145. };
  146. //初始化tts
  147. const tts = ref(null);
  148. const initTTS = () => {
  149. let params = {
  150. speed: settings.value.audioSpeed / 10, // 将 0-10 转换为 0-1
  151. pitch: settings.value.audioVolume, // 将 0-10 转换为 0-1
  152. volume: 1,
  153. };
  154. tts.value = new VolumeTTS(params);
  155. settings.value.audioStartSwitch &&
  156. tts.value.speak(settings.value.audioStart);
  157. };
  158. // 停止播放
  159. function stopPlayback() {
  160. tts.value && tts.value.stop();
  161. uni.showToast({
  162. title: "已停止播放",
  163. icon: "none",
  164. });
  165. }
  166. // 重启应用
  167. function restartApp() {
  168. uni.showModal({
  169. title: "提示",
  170. content: "确定要重启应用吗?",
  171. success: function (res) {
  172. if (res.confirm) {
  173. // #ifdef APP-PLUS
  174. plus.runtime.restart();
  175. // #endif
  176. // #ifdef H5 || MP-WEIXIN
  177. uni.reLaunch({
  178. url: "/pages/index/index",
  179. });
  180. // #endif
  181. }
  182. },
  183. });
  184. }
  185. // 加载保存的设置
  186. function loadSettings() {
  187. try {
  188. const savedSettings = uni.getStorageSync("tts_settings");
  189. if (savedSettings) {
  190. Object.assign(settings, savedSettings);
  191. }
  192. } catch (error) {
  193. console.error("加载设置失败:", error);
  194. }
  195. }
  196. // 播放点击音效
  197. function playClickSound() {
  198. if (!settings.audioClick) return;
  199. const audio = uni.createInnerAudioContext();
  200. audio.src = "/static/audio/click.mp3"; // 确保添加点击音效文件
  201. audio.play();
  202. }
  203. // 生命周期钩子
  204. onMounted(async () => {
  205. loadSettings();
  206. });
  207. onUnmounted(() => {
  208. if (tts.value) {
  209. tts.value.destroy?.();
  210. }
  211. });
  212. </script>
  213. <style lang="scss" scoped>
  214. .settings-container {
  215. min-height: 100vh;
  216. background-color: #f5f5f5;
  217. padding-bottom: 40rpx;
  218. .card-head {
  219. display: flex;
  220. align-items: center;
  221. padding: 10rpx 0;
  222. .head-title {
  223. margin-left: 10rpx;
  224. font-size: 32rpx;
  225. font-weight: bold;
  226. }
  227. }
  228. .settings-section {
  229. margin: 20rpx;
  230. background-color: #ffffff;
  231. border-radius: 12rpx;
  232. padding: 20rpx;
  233. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  234. }
  235. .input-section {
  236. margin: 30rpx 0;
  237. padding: 0 20rpx;
  238. }
  239. .slider-section {
  240. margin: 30rpx 0;
  241. padding: 0 20rpx;
  242. .slider-title {
  243. margin-bottom: 20rpx;
  244. font-size: 28rpx;
  245. color: #333;
  246. .subtitle {
  247. font-size: 24rpx;
  248. color: #999;
  249. margin-left: 10rpx;
  250. }
  251. }
  252. }
  253. .button-group {
  254. margin-top: 50rpx;
  255. padding: 0 20rpx;
  256. :deep(.u-button) {
  257. margin-bottom: 20rpx;
  258. }
  259. }
  260. }
  261. </style>