useAudioRightWrong.js 966 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. let audioContext = null;
  2. let code = null; // 身份标识
  3. audioContext = uni.createInnerAudioContext(); // 单例模式[3](@ref)
  4. audioContext.onEnded(() => {
  5. // 播放结束
  6. uni.$emit('result-audio-ended', code)
  7. })
  8. audioContext.onPlay(() => {
  9. // 播放
  10. uni.$emit('result-audio-play', code);
  11. });
  12. const audioList = {
  13. right: '/static/mp3/newYingyu/right-tip.mp3',
  14. wrong: '/static/mp3/newYingyu/error-tip.mp3'
  15. }
  16. export const resultImageList = {
  17. right: '/static/images/study/cjdc/right-tip-img-gif',
  18. wrong: '/static/images/study/cjdc/error-tip-img-gif'
  19. }
  20. export class resultAudioPlayer {
  21. // 播放音频
  22. play(codeT, code1) {
  23. code = {
  24. code1,
  25. codeT
  26. };
  27. if (audioContext.src === audioList[codeT] && !audioContext.paused) return;
  28. audioContext.src = audioList[codeT];
  29. audioContext.play();
  30. }
  31. // 暂停播放
  32. pause() {
  33. audioContext?.pause();
  34. }
  35. // 停止播放(释放资源)
  36. stop() {
  37. audioContext?.stop();
  38. audioContext = null;
  39. }
  40. }