audio.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Howl } from "howler";
  2. import { ref, watch, ComputedRef } from "vue";
  3. /**语音设置 */
  4. export function useAudioSet(currentSubject: ComputedRef<any>) {
  5. const aotuPlayFlag = ref(false);
  6. let sound: Howl;
  7. /**
  8. * 播放音频
  9. * @param audioUrl
  10. */
  11. const audioPlay = (audioUrl: string | string[]) => {
  12. audioPause();
  13. sound = new Howl({
  14. src: audioUrl,
  15. });
  16. sound.once("load", function () {
  17. sound.play();
  18. });
  19. if (typeof audioUrl === "object") {
  20. sound.once("end", () => {
  21. sound = new Howl({
  22. src: audioUrl[1],
  23. });
  24. sound.once("load", function () {
  25. sound.play();
  26. });
  27. });
  28. }
  29. };
  30. /**
  31. * 读题
  32. */
  33. const subjectAudioPlay = (type: "读题" | "读官方解释" | "读技巧解释" | "读题+答案") => {
  34. switch (type) {
  35. case "读题":
  36. currentSubject.value.issuemp3 && audioPlay(currentSubject.value.issuemp3);
  37. break;
  38. case "读官方解释":
  39. currentSubject.value.explainjsmp3 && audioPlay(currentSubject.value.explainjsmp3);
  40. break;
  41. case "读技巧解释":
  42. currentSubject.value.explainMp3 && audioPlay(currentSubject.value.explainMp3);
  43. break;
  44. case "读题+答案":
  45. currentSubject.value.issuemp3 && currentSubject.value.answermp3 && audioPlay([currentSubject.value.issuemp3, currentSubject.value.answermp3]);
  46. break;
  47. default:
  48. break;
  49. }
  50. };
  51. /**
  52. * 停止播放
  53. */
  54. const audioPause = () => {
  55. sound && sound.pause();
  56. };
  57. //音频模块end
  58. const aotuPlaySet = () => {
  59. aotuPlayFlag.value = !aotuPlayFlag.value;
  60. aotuPlayFlag.value ? subjectAudioPlay("读题") : audioPause();
  61. };
  62. //自动读题
  63. watch(currentSubject, () => {
  64. if (aotuPlayFlag.value) subjectAudioPlay("读题"); //自动读题
  65. });
  66. return {
  67. aotuPlayFlag,
  68. aotuPlaySet,
  69. subjectAudioPlay,
  70. };
  71. }