audio.js 866 B

1234567891011121314151617181920212223242526272829303132333435
  1. // audioUtils.js
  2. let audioContext = null;
  3. export function playVoice(url) {
  4. if (audioContext) {
  5. try {
  6. audioContext.pause();
  7. audioContext.destroy();
  8. audioContext = null;
  9. } catch (e) {
  10. // TODO: Handle the exception
  11. }
  12. audioContext = uni.createInnerAudioContext();
  13. audioContext.autoplay = true;
  14. audioContext.src = url;
  15. audioContext.onPlay(() => {
  16. console.log('开始播放');
  17. });
  18. } else {
  19. audioContext = uni.createInnerAudioContext();
  20. audioContext.autoplay = true;
  21. audioContext.src = url;
  22. audioContext.onPlay(() => {
  23. console.log('开始播放');
  24. });
  25. }
  26. }
  27. export const audioUrls = {
  28. failVoiceUrl: '../../static/fail.mp3',
  29. allSuccessVoiceUrl: '../../static/allSuccess.mp3',
  30. successVoiceUrl: '../../static/success.mp3',
  31. success2VoiceUrl: '../../static/success2.mp3'
  32. }