jessibuca-plugin.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Jessibuca视频播放器插件
  3. * 用于初始化Jessibuca所需的脚本和依赖
  4. * Vue 3 版本 - 使用条件编译支持跨平台
  5. */
  6. function loadJessibucaScript() {
  7. return new Promise((resolve, reject) => {
  8. // #ifdef H5
  9. if (typeof window === 'undefined') {
  10. return reject(new Error('非浏览器环境,无法加载Jessibuca'))
  11. }
  12. if (window.Jessibuca) {
  13. return resolve(window.Jessibuca)
  14. }
  15. const script = document.createElement('script')
  16. script.src = './static/js/jessibuca/jessibuca.js'
  17. script.async = true
  18. script.onload = () => {
  19. console.log('Jessibuca脚本加载成功')
  20. resolve(window.Jessibuca)
  21. }
  22. script.onerror = (error) => {
  23. console.error('Jessibuca脚本加载失败:', error)
  24. // 尝试使用备用路径
  25. const backupScript = document.createElement('script')
  26. backupScript.src = '/static/js/jessibuca/jessibuca.js'
  27. backupScript.async = true
  28. backupScript.onload = () => {
  29. console.log('Jessibuca备用脚本加载成功')
  30. resolve(window.Jessibuca)
  31. }
  32. backupScript.onerror = (backupError) => {
  33. console.error('Jessibuca备用脚本加载失败:', backupError)
  34. reject(backupError)
  35. }
  36. document.head.appendChild(backupScript)
  37. }
  38. document.head.appendChild(script)
  39. // #endif
  40. // #ifndef H5
  41. // 非H5平台不支持Jessibuca
  42. reject(new Error('Jessibuca仅支持H5平台'))
  43. // #endif
  44. })
  45. }
  46. // 判断是否支持 Jessibuca(H5环境)
  47. function isSupported() {
  48. // #ifdef H5
  49. return typeof window !== 'undefined' && typeof document !== 'undefined'
  50. // #endif
  51. }
  52. export default {
  53. install(app) {
  54. // Vue 3 使用 app.config.globalProperties 替代 Vue.prototype
  55. app.config.globalProperties.$jessibuca = {
  56. loadScript: loadJessibucaScript,
  57. createPlayer(options) {
  58. return loadJessibucaScript().then((Jessibuca) => {
  59. if (!Jessibuca) {
  60. console.log('Jessibuca 加载失败,无法创建播放器')
  61. return null
  62. }
  63. console.log("options你大爷",options);
  64. // 确保设置了decoder和wasm路径
  65. options = options || {};
  66. options.decoder = options.decoder || './static/js/jessibuca/decoder.js';
  67. options.wasmUrl = options.wasmUrl || 'https://nxy.gbdfarm.com:9000/jessibuca/decoder.wasm';
  68. return new Jessibuca(options)
  69. })
  70. },
  71. isSupported
  72. }
  73. // 预加载脚本(仅H5环境下执行)
  74. // #ifdef H5
  75. if (isSupported()) {
  76. setTimeout(() => {
  77. loadJessibucaScript().catch(err => {
  78. console.error('预加载 Jessibuca 失败:', err)
  79. })
  80. }, 1000)
  81. }
  82. // #endif
  83. }
  84. }