j-video.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="root" :style="{width,height}">
  3. <image :style="{width,height}" class="posterImg" :src="posterUrl" mode="aspectFit"></image>
  4. <view :style="{width,height}" @click="state=!state" class="box">
  5. <image class="playIcon" :src="imgUrl+'/play.png'" mode="widthFix"></image>
  6. </view>
  7. <video :id="videoId" :style="{height,width:state?'750rpx':'1rpx'}" @pause="state=0" @timeupdate="timeupdate" @fullscreenchange="fullscreenchange" class="video" :src="url"></video>
  8. <!-- <progress :style="{'top':height,width}" class="progress" :percent="currentTime?parseInt(currentTime/duration*100):0" show-info border-radius="5" active></progress> -->
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. computed:{
  14. posterUrl(){
  15. if(this.poster) return this.poster
  16. return this.url + '?x-oss-process=video/snapshot,t_'+(parseInt(this.currentTime*1000))+',f_jpg,w_800,m_fast,ar_auto'
  17. }
  18. },
  19. created() {
  20. this.videoId = Date.now() + Math.ceil(Math.random()*10000000)+"";
  21. },
  22. mounted() {
  23. this.VideoContext = uni.createVideoContext(this.videoId)
  24. },
  25. methods:{
  26. fullscreenchange(e){
  27. console.log(e.detail.fullScreen);
  28. this.state = e.detail.fullScreen
  29. },
  30. timeupdate(e){
  31. console.log(e.detail);
  32. this.duration = e.detail.duration
  33. this.currentTime = e.detail.currentTime
  34. }
  35. },
  36. watch: {
  37. state(state, oldValue) {
  38. console.log(state,'state');
  39. if(!state){
  40. this.VideoContext.pause()
  41. }else{
  42. this.VideoContext.play()
  43. this.$nextTick(()=>{
  44. this.VideoContext.requestFullScreen({direction:this.direction})
  45. })
  46. }
  47. }
  48. },
  49. data() {
  50. return {
  51. VideoContext:{},
  52. state:false,
  53. currentTime:0,
  54. duration:0,
  55. videoId:'',
  56. imgUrl: this.$mConfig.staticUrl
  57. };
  58. },
  59. props: {
  60. poster: {
  61. type: [String,Boolean],
  62. default(){
  63. return ''
  64. }
  65. },
  66. url: {
  67. type: String,
  68. default(){
  69. return ''
  70. }
  71. },
  72. direction: {
  73. type: Number,
  74. default(){
  75. return 0
  76. }
  77. },
  78. width: {
  79. type: String,
  80. default(){
  81. return "750rpx";
  82. }
  83. },
  84. height: {
  85. type: String,
  86. default(){
  87. return "450rpx";
  88. }
  89. }
  90. },
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. /deep/.uni-video-container{
  95. width: 0;
  96. height: 0;
  97. }
  98. .root{
  99. position:relative;
  100. width: 750rpx;
  101. height: 300px;
  102. overflow: hidden;
  103. }
  104. .posterImg,.video,.box{
  105. display: flex;
  106. width: 750rpx;
  107. height: 300px;
  108. //border: solid 1px red;absolute
  109. position:absolute;
  110. }
  111. .posterImg{
  112. //border: solid red 1px;
  113. }
  114. .box{
  115. justify-content: center;
  116. align-items: center;
  117. }
  118. .playIcon{
  119. width: 100rpx !important;
  120. }
  121. </style>