jessibuca-plugin.js 2.4 KB

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