vi.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <view class="video">
  3. <video :src="src" :controls="controls" :show-play-btn="false" :muted="!load||!play"
  4. :style="{ height: height }" :loop="true" @play="continuePlay"
  5. :enable-progress-gesture="false" :objectFit="objectFit" @ended="playEnd"
  6. :initial-time="startTime"
  7. :id="`video_${src}`" :ref="`video_${src}`" class="video" @timeupdate="timeupdate">
  8. </video>
  9. </view>
  10. </template>
  11. <script>
  12. export default{
  13. components:{
  14. },
  15. props:{
  16. objectFit:{
  17. type:String,
  18. default:'contain'
  19. },
  20. controls:{
  21. type:Boolean,
  22. default:false
  23. },
  24. play:{
  25. type:Boolean,
  26. default:false
  27. },
  28. initialTime:{
  29. type:Number,
  30. default:0
  31. },
  32. src:{
  33. type:String,
  34. default:''
  35. },
  36. height:{
  37. type:String,
  38. default:''
  39. },
  40. },
  41. data(){
  42. return{
  43. load:false,
  44. videoCtx: null,
  45. playFirst:true,
  46. timer:null
  47. }
  48. },
  49. beforeCreate() {
  50. },
  51. mounted() {
  52. this.videoCtx = uni.createVideoContext(`video_${this.src}`,this)
  53. //#ifndef MP-WEIXIN
  54. setTimeout(()=>{this.videoCtx.play();},200)
  55. // #endif
  56. },
  57. methods:{
  58. continuePlay() {
  59. if(!this.load){
  60. setTimeout(()=>{
  61. this.load = true
  62. this.videoPlay()
  63. },1000)
  64. }
  65. },
  66. playEnd() {},
  67. timeupdate() {},
  68. videoPlay:function(){
  69. if(this.timer) clearTimeout(this.timer)
  70. this.timer = setTimeout(()=>{
  71. if(this.play){
  72. this.videoCtx.play();
  73. this.playFirst = false
  74. }else{
  75. this.videoCtx.pause();
  76. this.$emit('pause',this.time)
  77. }
  78. })
  79. },
  80. },
  81. watch:{
  82. play: function (newVal,oldVal){
  83. if(this.load) this.videoPlay()
  84. }
  85. },
  86. computed:{
  87. startTime(){
  88. return this.initialTime
  89. }
  90. }
  91. }
  92. </script>
  93. <style scoped lang="scss">
  94. .video{
  95. display: flex;
  96. width: 750rpx;
  97. justify-content: center;
  98. align-items: center;
  99. }
  100. </style>