MyVideo.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="myVideo_view" v-html="innerHtml" :id="id" :change:id="MyVideo.updateId" :isPlay="isPlay" :change:isPlay="MyVideo.handelPlay" ></view>
  3. </template>
  4. <script>
  5. export default {
  6. props: {
  7. item: {
  8. type: Object,
  9. default: () => ({}),
  10. },
  11. // 添加控制播放和暂停的prop
  12. isPlay: {
  13. type: Boolean,
  14. default: true,
  15. },
  16. },
  17. computed: {
  18. id() {
  19. return this.item.id
  20. }
  21. },
  22. data() {
  23. return {
  24. innerHtml: '',
  25. };
  26. },
  27. created() {
  28. this.initVideoHtml();
  29. },
  30. methods: {
  31. isHttpOrHttps(url) {
  32. const regex = /^(http|https):\/\//;
  33. return regex.test(url);
  34. },
  35. initVideoHtml() {
  36. let { url } = this.item
  37. let bool = this.isHttpOrHttps(url)
  38. if(!bool) {
  39. url = plus.io.convertLocalFileSystemURL(url);
  40. }
  41. this.innerHtml = `<video class="swiper-video" id="Video${this.item.id}" src="${url}" width="100%" height="100%" style="object-fit: contain;" autoplay/>`;
  42. },
  43. // 通知父组件播放完成
  44. ended() {
  45. this.$emit('onEnded')
  46. }
  47. },
  48. };
  49. </script>
  50. <script module="MyVideo" lang="renderjs">
  51. export default {
  52. data() {
  53. return {
  54. id: '',
  55. video: null,
  56. }
  57. },
  58. computed: {
  59. videoId() {
  60. return 'Video' + this.id
  61. }
  62. },
  63. mounted() {
  64. this.initVideoElement()
  65. },
  66. methods: {
  67. initVideoElement() {
  68. let video = document.getElementById(this.videoId)
  69. this.video = video
  70. video.addEventListener('loadedmetadata', () => {
  71. this.video.play().then(res => {
  72. this.video.pause()
  73. })
  74. })
  75. video.addEventListener('ended', () => {
  76. this.video.pause()
  77. this.$ownerInstance.callMethod('ended')
  78. })
  79. },
  80. handelPlay(isPlay) {
  81. if(!this.video) return
  82. isPlay ? this.video.play() : this.video.pause()
  83. },
  84. updateId(id) {
  85. this.id = id
  86. },
  87. }
  88. }
  89. </script>
  90. <style scoped>
  91. .myVideo_view {
  92. height: 100%;
  93. border-radius: 10rpx;
  94. overflow: hidden;
  95. background-color: #000;
  96. }
  97. </style>