inner-audio-path.uvue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <page-head :title="title"></page-head>
  3. <view class="uni-padding-wrap uni-common-mt">
  4. <view class="uni-title">
  5. <text class="uni-title-text">音频路径示例</text>
  6. </view>
  7. <view class="formats" v-for="(item,index) in supportPaths" :key="index">
  8. <text class="uni-subtitle-text">{{item.description}}</text>
  9. <image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
  10. @click="play(item.src,index)"></image>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. type AudioPath = {
  16. description : string
  17. src : string
  18. }
  19. export default {
  20. data() {
  21. return {
  22. title: 'audio-path',
  23. playIndex: 0,
  24. isPlaying: false,
  25. // #ifdef APP
  26. nativePath: uni.env.CACHE_PATH + 'uni-audio/test/test.mp3' as string,
  27. // sdcardPath: 'sdcard/uni-audio/test.mp3',
  28. // #endif
  29. _audioContext: null as InnerAudioContext | null,
  30. supportPaths: [
  31. {
  32. description: '本地路径:/static方式',
  33. src: '/static/test-audio/ForElise.mp3'
  34. },
  35. {
  36. description: '本地路径:../static/',
  37. src: '../../../static/test-audio/ForElise.mp3'
  38. },
  39. // #ifdef APP
  40. {
  41. description: '本地路径:env方式',
  42. src: 'env'
  43. },
  44. // #endif
  45. {
  46. description: '网络路径',
  47. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3'
  48. },
  49. {
  50. description: '不存在的音频',
  51. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/invalid_url.mp3'
  52. },
  53. {
  54. description: '错误路径',
  55. src: '../static/test-audio/ForElise22.mp3'
  56. },
  57. ] as Array<AudioPath>
  58. }
  59. },
  60. onReady() {
  61. this._audioContext = uni.createInnerAudioContext();
  62. this._audioContext!.onPlay(() => {
  63. console.log('开始播放');
  64. });
  65. this._audioContext!.onEnded(() => {
  66. console.log('播放结束');
  67. this.isPlaying = false;
  68. });
  69. this._audioContext!.onError((err) => {
  70. this.isPlaying = false;
  71. console.log('err', err);
  72. });
  73. // #ifdef APP
  74. const fileManager = uni.getFileSystemManager()
  75. fileManager.mkdir({
  76. dirPath: uni.env.CACHE_PATH + 'uni-audio/test',
  77. recursive: true,
  78. success: (res) => {
  79. fileManager.copyFile({
  80. srcPath: '/static/test-audio/ForElise.mp3',
  81. destPath: this.nativePath,
  82. success: () => {
  83. console.log("copy成功: ", res)
  84. }
  85. })
  86. },
  87. fail: (err) => {
  88. console.log("创建路径失败: ", err.errMsg)
  89. if (err.errMsg.includes("file already exists")) {
  90. console.log("已经包含该路径")
  91. fileManager.copyFile({
  92. srcPath: '/static/test-audio/ForElise.mp3',
  93. destPath: this.nativePath,
  94. success: (res) => {
  95. console.log("copy成功: ", res)
  96. },
  97. fail: (err) => {
  98. console.log("copy失败: ", err)
  99. }
  100. })
  101. }
  102. }
  103. })
  104. // #endif
  105. },
  106. onUnload() {
  107. if (this._audioContext != null) {
  108. this.pause();
  109. this._audioContext!.destroy()
  110. }
  111. },
  112. methods: {
  113. pause() {
  114. this._audioContext!.pause();
  115. this.isPlaying = false;
  116. },
  117. play(audioUrl : string, index : number) {
  118. console.log(index, audioUrl);
  119. if (this.isPlaying && this.playIndex == index) {
  120. this.pause();
  121. return;
  122. }
  123. // #ifdef APP
  124. if (audioUrl == 'env') {
  125. audioUrl = this.nativePath
  126. }
  127. // #endif
  128. this.playIndex = index
  129. this._audioContext!.src = audioUrl;
  130. this._audioContext!.play();
  131. this.isPlaying = true;
  132. }
  133. }
  134. }
  135. </script>
  136. <style>
  137. .formats {
  138. align-items: center;
  139. }
  140. .icon-play {
  141. width: 60px;
  142. height: 60px;
  143. margin: 10px;
  144. }
  145. </style>