59b7d2fa83c3bfc61632b441f09654baa9edb917.svn-base 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <div class="di main-wrap" style="border:1px dotted rgb(198, 226, 255);padding:0px;border-radius: 15px;padding-left:10px;padding-right:10px;">
  3. <!-- 这里设置了ref属性后,在vue组件中,就可以用this.$refs.audio来访问该dom元素 -->
  4. <audio ref="audio" class="dn"
  5. :src="url" :preload="audio.preload"
  6. @play="onPlay"
  7. @error="onError"
  8. @waiting="onWaiting"
  9. @pause="onPause"
  10. @timeupdate="onTimeupdate"
  11. @loadedmetadata="onLoadedmetadata"
  12. ></audio>
  13. <div style="height:100%;width:100%;display:flex;flex-direction:row;justify-content:center;align-items:center;">
  14. <el-button type="text" @click="startPlayOrPause">{{audio.playing | transPlayPause}}</el-button>
  15. <!-- <el-button v-show="!controlList.noSpeed" type="text" @click="changeSpeed">{{audio.speed | transSpeed}}</el-button> -->
  16. <el-tag type="info" style="margin:0px;">{{ audio.currentTime | formatSecond}}</el-tag>
  17. <el-slider v-show="!controlList.noProcess" v-model="sliderTime" :format-tooltip="formatProcessToolTip" @change="changeCurrentTime" class="slider"></el-slider>
  18. <el-tag type="info" style="margin:0px;">{{ audio.maxTime | formatSecond }}</el-tag>
  19. <!-- <el-button v-show="!controlList.noMuted" type="text" @click="startMutedOrNot">{{audio.muted | transMutedOrNot}}</el-button> -->
  20. <!-- <el-slider v-show="!controlList.noVolume" v-model="volume" :format-tooltip="formatVolumeToolTip" @change="changeVolume" class="slider"></el-slider>
  21. <a :href="url" v-show="!controlList.noDownload" target="_blank" class="download" download>下载</a> -->
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. function realFormatSecond(second) {
  27. var secondType = typeof second
  28. if (secondType === 'number' || secondType === 'string') {
  29. second = parseInt(second)
  30. var hours = Math.floor(second / 3600)
  31. second = second - hours * 3600
  32. var mimute = Math.floor(second / 60)
  33. second = second - mimute * 60
  34. return hours + ':' + ('0' + mimute).slice(-2) + ':' + ('0' + second).slice(-2)
  35. } else {
  36. return '0:00:00'
  37. }
  38. }
  39. export default {
  40. props: {
  41. name:'',
  42. theUrl: {
  43. type: String,
  44. required: true,
  45. },
  46. theSpeeds: {
  47. type: Array,
  48. default () {
  49. return [1, 1.5, 2]
  50. }
  51. },
  52. theControlList: {
  53. type: String,
  54. default: ''
  55. }
  56. },
  57. name: 'VueAudio',
  58. data() {
  59. return {
  60. url: this.theUrl || 'http://devtest.qiniudn.com/secret base~.mp3',
  61. audio: {
  62. currentTime: 0,
  63. maxTime: 0,
  64. playing: false,
  65. muted: false,
  66. speed: 1,
  67. waiting: true,
  68. preload: 'auto'
  69. },
  70. sliderTime: 0,
  71. volume: 100,
  72. speeds: this.theSpeeds,
  73. controlList: {
  74. // 不显示下载
  75. noDownload: false,
  76. // 不显示静音
  77. noMuted: false,
  78. // 不显示音量条
  79. noVolume: false,
  80. // 不显示进度条
  81. noProcess: false,
  82. // 只能播放一个
  83. onlyOnePlaying: false,
  84. // 不要快进按钮
  85. noSpeed: false
  86. }
  87. }
  88. },
  89. methods: {
  90. setControlList () {
  91. let controlList = this.theControlList.split(' ')
  92. controlList.forEach((item) => {
  93. if(this.controlList[item] !== undefined){
  94. this.controlList[item] = true
  95. }
  96. })
  97. },
  98. changeSpeed() {
  99. let index = this.speeds.indexOf(this.audio.speed) + 1
  100. this.audio.speed = this.speeds[index % this.speeds.length]
  101. this.$refs.audio.playbackRate = this.audio.speed
  102. },
  103. startMutedOrNot() {
  104. this.$refs.audio.muted = !this.$refs.audio.muted
  105. this.audio.muted = this.$refs.audio.muted
  106. },
  107. // 音量条toolTip
  108. formatVolumeToolTip(index) {
  109. return '音量条: ' + index
  110. },
  111. // 进度条toolTip
  112. formatProcessToolTip(index = 0) {
  113. index = parseInt(this.audio.maxTime / 100 * index)
  114. return '进度条: ' + realFormatSecond(index)
  115. },
  116. // 音量改变
  117. changeVolume(index = 0) {
  118. this.$refs.audio.volume = index / 100
  119. this.volume = index
  120. },
  121. // 播放跳转
  122. changeCurrentTime(index) {
  123. this.$refs.audio.currentTime = parseInt(index / 100 * this.audio.maxTime)
  124. },
  125. startPlayOrPause() {
  126. return this.audio.playing ? this.pausePlay() : this.startPlay()
  127. },
  128. // 开始播放
  129. startPlay() {
  130. this.$refs.audio.play()
  131. },
  132. // 暂停
  133. pausePlay() {
  134. this.$refs.audio.pause()
  135. },
  136. // 当音频暂停
  137. onPause () {
  138. this.audio.playing = false
  139. },
  140. // 当发生错误, 就出现loading状态
  141. onError () {
  142. this.audio.waiting = true
  143. },
  144. // 当音频开始等待
  145. onWaiting (res) {
  146. console.log(res)
  147. },
  148. // 当音频开始播放
  149. onPlay (res) {
  150. console.log(res)
  151. this.audio.playing = true
  152. this.audio.loading = false
  153. if(!this.controlList.onlyOnePlaying){
  154. return
  155. }
  156. let target = res.target
  157. let audios = document.getElementsByTagName('audio');
  158. [...audios].forEach((item) => {
  159. if(item !== target){
  160. item.pause()
  161. }
  162. })
  163. },
  164. // 当timeupdate事件大概每秒一次,用来更新音频流的当前播放时间
  165. onTimeupdate(res) {
  166. // console.log('timeupdate')
  167. // console.log(res)
  168. this.audio.currentTime = res.target.currentTime
  169. this.sliderTime = parseInt(this.audio.currentTime / this.audio.maxTime * 100)
  170. },
  171. // 当加载语音流元数据完成后,会触发该事件的回调函数
  172. // 语音元数据主要是语音的长度之类的数据
  173. onLoadedmetadata(res) {
  174. console.log('loadedmetadata')
  175. console.log(res)
  176. this.audio.waiting = false
  177. this.audio.maxTime = parseInt(res.target.duration)
  178. }
  179. },
  180. filters: {
  181. formatSecond(second = 0) {
  182. return realFormatSecond(second)
  183. },
  184. transPlayPause(value) {
  185. return value ? '暂停' : '播放'
  186. },
  187. transMutedOrNot(value) {
  188. return value ? '放音' : '静音'
  189. },
  190. transSpeed(value) {
  191. return '快进: x' + value
  192. }
  193. },
  194. created() {
  195. this.setControlList()
  196. }
  197. }
  198. </script>
  199. <!-- Add "scoped" attribute to limit CSS to this component only -->
  200. <style scoped>
  201. .main-wrap{
  202. /* padding: 10px 15px; */
  203. }
  204. .slider {
  205. display: inline-block;
  206. width: 100px;
  207. /* position: relative; */
  208. top: 14px;
  209. margin-left: 15px;
  210. }
  211. .di {
  212. display: inline-block;
  213. }
  214. .download {
  215. color: #409EFF;
  216. /* margin-left: 15px; */
  217. }
  218. .dn{
  219. display: none;
  220. }
  221. </style>