useVideoPlay.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {ref, watch} from 'vue'
  2. import {createEventHook} from "@vueuse/core";
  3. import {getVideoPlayInfo} from "@/api/webApi/webVideo";
  4. export const useVideoPlay = function (videoProps) {
  5. const safeUrl = ref('')
  6. const coverUrl = ref('')
  7. const srcChange = createEventHook()
  8. const isPlayUrl = (src) => src?.toLowerCase().startsWith('http')
  9. watch([() => videoProps.src, () => videoProps.aliIdType], (n, o) => {
  10. // TODO: 目前还没有测试切换播放源的问题,以观后效。
  11. const oldVal = {src: o[0], aliIdType: o[1]}
  12. const newVal = {src: n[0], aliIdType: n[1]}
  13. if (isPlayUrl(newVal.src)) {
  14. safeUrl.value = newVal.src
  15. srcChange.trigger(newVal, oldVal).then()
  16. } else if (newVal.src) {
  17. // 假定这里的src就是aliId
  18. getVideoPlayInfo({videoId: newVal.src}).then(res => {
  19. const {palyUrl: play, coverUrl: cover} = res.data
  20. if (isPlayUrl(play)) {
  21. safeUrl.value = play
  22. coverUrl.value = cover
  23. srcChange.trigger(newVal, oldVal).then()
  24. }
  25. })
  26. }
  27. }, {immediate: true})
  28. return {
  29. safeUrl,
  30. coverUrl,
  31. onSrcChange: srcChange.on
  32. }
  33. }