j-video.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="root" :style="{width,height}">
  3. <image :style="{width,height}" class="posterImg" :src="posterUrl"></image>
  4. <view :style="{width,height}" @click="state=!state" class="box">
  5. <image class="playIcon" src="/static/images/icon_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'
  17. }
  18. },
  19. created() {
  20. this.videoId = Date.now() + Math.ceil(Math.random()*10000000)+"";
  21. },
  22. mounted() {
  23. this.VideoContext = uni.createVideoContext(this.videoId, this)
  24. },
  25. methods:{
  26. fullscreenchange(e){
  27. this.state = e.detail.fullScreen
  28. },
  29. timeupdate(e){
  30. this.duration = e.detail.duration
  31. this.currentTime = e.detail.currentTime
  32. }
  33. },
  34. watch: {
  35. state(state, oldValue) {
  36. //console.log(state,'state');
  37. if(!state){
  38. this.VideoContext.pause()
  39. }else{
  40. this.VideoContext.play()
  41. setTimeout(()=>{
  42. this.VideoContext.requestFullScreen({direction:this.direction})
  43. },10)
  44. }
  45. }
  46. },
  47. data() {
  48. return {
  49. VideoContext:{},
  50. state:false,
  51. currentTime:0,
  52. duration:0,
  53. videoId:''
  54. };
  55. },
  56. props: {
  57. poster: {
  58. type: [String,Boolean],
  59. default(){
  60. return ''
  61. }
  62. },
  63. url: {
  64. type: String,
  65. default(){
  66. return ''
  67. }
  68. },
  69. direction: {
  70. type: Number,
  71. default(){
  72. return 0
  73. }
  74. },
  75. width: {
  76. type: String,
  77. default(){
  78. return "750rpx";
  79. }
  80. },
  81. height: {
  82. type: String,
  83. default(){
  84. return "450rpx";
  85. }
  86. }
  87. },
  88. }
  89. </script>
  90. <style lang="scss" scoped>
  91. .root{
  92. position:relative;
  93. width: 750rpx;
  94. height: 300px;
  95. overflow: hidden;
  96. }
  97. .posterImg,.video,.box{
  98. display: flex;
  99. width: 750rpx;
  100. height: 300px;
  101. //border: solid 1px red;absolute
  102. position:absolute;
  103. }
  104. .video{
  105. margin-left: -2000px;
  106. }
  107. .posterImg{
  108. //border: solid red 1px;
  109. }
  110. .box{
  111. justify-content: center;
  112. align-items: center;
  113. }
  114. .playIcon{
  115. width: 100rpx;
  116. height: 100rpx;
  117. }
  118. </style>