jessibuca-plugin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // #ifndef H5
  52. return false
  53. // #endif
  54. }
  55. export default {
  56. install(app) {
  57. // Vue 3 使用 app.config.globalProperties 替代 Vue.prototype
  58. app.config.globalProperties.$jessibuca = {
  59. loadScript: loadJessibucaScript,
  60. createPlayer(options) {
  61. return loadJessibucaScript().then((Jessibuca) => {
  62. if (!Jessibuca) {
  63. console.log('Jessibuca 加载失败,无法创建播放器')
  64. return null
  65. }
  66. // 确保设置了decoder和wasm路径
  67. options = options || {};
  68. options.decoder = options.decoder || './static/js/jessibuca/decoder.js';
  69. options.wasmUrl = options.wasmUrl || './static/js/jessibuca/decoder.wasm';
  70. return new Jessibuca(options)
  71. })
  72. },
  73. isSupported
  74. }
  75. // 预加载脚本(仅H5环境下执行)
  76. // #ifdef H5
  77. if (isSupported()) {
  78. setTimeout(() => {
  79. loadJessibucaScript().catch(err => {
  80. console.error('预加载 Jessibuca 失败:', err)
  81. })
  82. }, 1000)
  83. }
  84. // #endif
  85. }
  86. }