inner-audio-format.uvue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <page-head :title="title"></page-head>
  3. <!-- #ifdef APP -->
  4. <scroll-view style="flex: 1;">
  5. <!-- #endif -->
  6. <view class="uni-padding-wrap uni-common-mt">
  7. <view class="formats" v-for="(item,index) in supportFormats" :key="index">
  8. <text class="uni-subtitle-text">{{item.format}}</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 class="formats" v-for="(item,index) in notSupportFormats" :key="index">
  13. <text class="uni-subtitle-text">{{ item.format + (item.support) }}</text>
  14. <image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
  15. @click="play(item.src,index)"></image>
  16. </view>
  17. </view>
  18. <!-- #ifdef APP -->
  19. </scroll-view>
  20. <!-- #endif -->
  21. </template>
  22. <script>
  23. type AudioFormat = {
  24. format : string
  25. support: string | null
  26. src : string
  27. }
  28. export default {
  29. data() {
  30. return {
  31. title: 'audio-format',
  32. playIndex: 0,
  33. isPlaying: false,
  34. _audioContext: null as InnerAudioContext | null,
  35. supportFormats: [
  36. {
  37. format: 'mp3',
  38. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3'
  39. },
  40. {
  41. format: 'mp4',
  42. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp4'
  43. },
  44. {
  45. format: 'm4a',
  46. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.m4a'
  47. },
  48. {
  49. format: 'aac',
  50. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aac'
  51. },
  52. {
  53. format: 'flac',
  54. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.flac'
  55. },
  56. {
  57. format: 'wav',
  58. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wav'
  59. },
  60. {
  61. format:"m3u8",
  62. src:'https://qiniu-web-assets.dcloud.net.cn/audio/sample/m3u8/ForElise.m3u8'
  63. }
  64. ] as Array<AudioFormat>,
  65. notSupportFormats: [
  66. {
  67. format: 'ogg',
  68. support: '(iOS 不支持)',
  69. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.ogg'
  70. },
  71. {
  72. format: 'wma',
  73. support: '(iOS/Android/web/Harmony 不支持)',
  74. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wma'
  75. },
  76. {
  77. format: 'aiff',
  78. support: '(Android/web/Harmony 不支持)',
  79. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aiff'
  80. },
  81. {
  82. format: 'caf',
  83. support: '(Android/web/Harmony 不支持)',
  84. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.caf'
  85. },
  86. {
  87. format:"dash",
  88. support: '(iOS 不支持)',
  89. src:'https://qiniu-web-assets.dcloud.net.cn/audio/sample/dash/ForElise.mpd'
  90. },
  91. {
  92. format: '错误格式',
  93. support: '(iOS/Android/web/Harmony 不支持)',
  94. src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wmaa'
  95. },
  96. ] as Array<AudioFormat>
  97. }
  98. },
  99. onReady() {
  100. this._audioContext = uni.createInnerAudioContext();
  101. this._audioContext!.onPlay(() => {
  102. console.log('开始播放');
  103. });
  104. this._audioContext!.onPause(() => {
  105. console.log('播放暂停');
  106. })
  107. this._audioContext!.onEnded(() => {
  108. console.log('播放结束');
  109. this.isPlaying = false;
  110. });
  111. this._audioContext!.onError((err) => {
  112. this.isPlaying = false;
  113. console.log('err', err);
  114. });
  115. },
  116. onUnload() {
  117. if (this._audioContext != null) {
  118. this.pause();
  119. this._audioContext!.destroy()
  120. }
  121. },
  122. methods: {
  123. pause() {
  124. this._audioContext!.pause();
  125. this.isPlaying = false;
  126. },
  127. play(audioUrl : string, index : number) {
  128. // console.log(index,audioUrl);
  129. if (this.isPlaying && this.playIndex == index) {
  130. this.pause();
  131. return;
  132. }
  133. this.playIndex = index
  134. this._audioContext!.src = audioUrl;
  135. this._audioContext!.play();
  136. this.isPlaying = true;
  137. },
  138. },
  139. }
  140. </script>
  141. <style>
  142. .formats {
  143. align-items: center;
  144. }
  145. .icon-play {
  146. width: 60px;
  147. height: 60px;
  148. margin: 10px;
  149. }
  150. </style>