index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="audo-video">
  3. <video id="myVideo" :src="url" class="hidden" @timeupdate="timeupdate" ref="video" @loadedmetadata="loadedmetadata" ></video>
  4. <view class="slider-box">
  5. <text class="mm">{{timer}}</text>
  6. <slider
  7. style="width: 500upx;"
  8. @change="sliderChange"
  9. @changing="sliderChanging"
  10. class="audio-slider"
  11. block-size="16"
  12. :min="0"
  13. :max="duration"
  14. :value="currentTime"
  15. :activeColor=themeColor
  16. @touchstart="lock= true"
  17. @touchend="lock = false"
  18. />
  19. <text class="ss">{{overTimer}}</text>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. url: '',
  27. duration:Number
  28. },
  29. data() {
  30. return {
  31. lock: false, // 锁
  32. status: 1, // 1暂停 2播放
  33. currentTime: 0, //当前进度
  34. videoContext: '',
  35. themeColor:"",
  36. }
  37. },
  38. computed:{
  39. timer() {
  40. return calcTimer(this.currentTime)
  41. },
  42. overTimer() {
  43. return calcTimer(this.duration)
  44. }
  45. },
  46. created() {
  47. this.videoContext = uni.createVideoContext('myVideo')
  48. },
  49. mounted() {
  50. let app = getApp();
  51. this.themeColor = app.globalData.themeColor;
  52. },
  53. methods: {
  54. // 倍速
  55. setRate(num) {
  56. this.videoContext.playbackRate(num)
  57. },
  58. // 播放
  59. play() {
  60. this.status = 2
  61. this.videoContext.play()
  62. },
  63. playOrStop(){
  64. if(this.status == 2){
  65. this.stop();
  66. }else{
  67. this.play();
  68. }
  69. this.$emit("playStatus",this.status)
  70. },
  71. // 暂停
  72. stop() {
  73. this.videoContext.pause()
  74. this.status = 1
  75. },
  76. // 更新进度条
  77. timeupdate(event) {
  78. if(this.lock) return; // 锁
  79. var currentTime,duration;
  80. if(event.detail.detail) {
  81. currentTime = event.detail.detail.currentTime
  82. duration = event.detail.detail.duration
  83. }else {
  84. currentTime = event.detail.currentTime
  85. duration = event.detail.duration
  86. }
  87. this.currentTime = currentTime
  88. this.duration = duration
  89. },
  90. // 拖动进度条
  91. sliderChange(data) {
  92. this.videoContext.seek(data.detail.value)
  93. },
  94. //拖动中
  95. sliderChanging(data) {
  96. this.currentTime = data.detail.value
  97. },
  98. // 视频加载完成
  99. loadedmetadata(data) {
  100. this.duration = data.detail.duration
  101. }
  102. }
  103. }
  104. function calcTimer(timer) {
  105. if(timer === 0 || typeof timer !== 'number') {
  106. return '00:00'
  107. }
  108. let mm = Math.floor(timer / 60)
  109. let ss = Math.floor(timer % 60)
  110. if(mm < 10) {
  111. mm = '0' + mm
  112. }
  113. if(ss < 10) {
  114. ss = '0' + ss
  115. }
  116. return mm + ':' + ss
  117. }
  118. </script>
  119. <style scoped lang="scss">
  120. .audo-video {
  121. padding-bottom: 20upx;
  122. }
  123. .slider-box {
  124. display: flex;
  125. align-items: center;
  126. justify-content: center;
  127. font-size: 26upx;
  128. color: #999;
  129. }
  130. button {
  131. display: inline-block;
  132. width: 100upx;
  133. background-color: #fff;
  134. font-size: 24upx;
  135. color: #000;
  136. padding: 0;
  137. }
  138. .hidden {
  139. position: fixed;
  140. z-index: -1;
  141. width: 1upx;height: 1upx;
  142. }
  143. </style>