audio2.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. let audioContext = null;
  2. export function playVoice(url) {
  3. if (audioContext) {
  4. try {
  5. audioContext.pause();
  6. audioContext.destroy();
  7. audioContext = null;
  8. } catch (e) {
  9. // TODO: Handle the exception
  10. }
  11. }
  12. audioContext = uni.createInnerAudioContext();
  13. audioContext.autoplay = true;
  14. audioContext.src = url;
  15. audioContext.onPlay(() => {
  16. console.log('开始播放');
  17. });
  18. }
  19. export function playSequentially(audioUrls) {
  20. if (audioUrls.length === 0) return;
  21. let currentIndex = 0;
  22. function playNext() {
  23. if (currentIndex < audioUrls.length) {
  24. playVoice(audioUrls[currentIndex]);
  25. audioContext.onEnded(() => {
  26. currentIndex++;
  27. playNext(); // 播放下一个音频
  28. });
  29. } else {
  30. audioContext.destroy(); // 播放完毕,清理
  31. audioContext = null;
  32. }
  33. }
  34. playNext(); // 开始播放
  35. }
  36. export const audioUrls = {
  37. failVoiceUrl: '../../static/fail.mp3',
  38. allSuccessVoiceUrl: '../../static/allSuccess.mp3',
  39. successVoiceUrl: '../../static/success.mp3',
  40. success2VoiceUrl: '../../static/success2.mp3',
  41. storeSuccessUrl: '../../static/storeSuccess.mp3'
  42. };