将 shaka-player 包装在 AngularJS 指令中不起作用this.target.addEventListener 不是函数

问题描述

我想将 shaka-player example 包装在 AngularJS 指令中。

示例播放器本身运行良好,可以播放 Big Buck Bunny hosted on Akamai's CDN 的 MPEG-DASH 版本。

但是,当尝试在 AngularJS 指令中使用 shaka-player 时,player.load(url) 失败并显示 this.target.addEventListener is not a function

它还报告 TypeError: Cannot read property 'textTracks' of null。 (根据浏览器的不同,这些错误的顺序有时会颠倒)

我不知道为什么它可以独立运行,但不能在 AngularJS 指令中运行...

指令的 HTML 片段如下所示:

<tq-video-player
  tqsrc="source"
  poster="somePicture"
  id="my-video"
  width="640"
  height="360">
</tq-video-player>

指令代码本身几乎是从示例代码复制粘贴的。 (我还尝试了几种变体来处理 promise 而不是 async 函数。所有都有相同的错误

var Tquilib = Tquilib || angular.module('tquilib',[]);

Tquilib.directive("tqVideoPlayer",function($timeout,$parse)
{ 
    var directive =
    {
        restrict: 'E',template: '<video controls width="640"></video>',scope: 
        {
            tqsrc:  '=',poster: '=',width:  '=',height: '='
        },link: function(scope,element,attrs)
        {
            console.log("tqVideoPlayer link. nodename",element[0].nodeName,element);
        
            // Install built-in polyfills to patch browser incompatibilities.
            shaka.polyfill.installAll();

            // Check to see if the browser supports the basic APIs Shaka needs.
            if (!shaka.Player.isbrowserSupported())
            {
                // This browser does not have the minimum set of APIs we need.
                console.error('browser not supported!');
            
                let errDiv = angular.element("<p class='tqVidplayerErrDiv'>Video player not supported in this browser</p>");
                element.append(errDiv);
            
                throw new Error('Video player not supported in this browser');
            }
    
            let videoTag = angular.element(element).find("video");
            shaka.log.setLevel(shaka.log.DEBUG);
        
            var player = new shaka.Player(videoTag);
            player.addEventListener('error',errorEventHandler);
        
            let srcUrl = "https://dash.akamaized.net/akamai/bbb_30fps/bbb_30fps.mpd";
            console.log("Loading...",srcUrl);
        
            loadplayer(srcUrl);
        
            async function loadplayer(srcUrl)
            {
                console.log("loadplayer()");
                try
                {
                    await player.load(srcUrl);
                    console.log("Video is loaded");                 
                }
                catch (e)
                {
                    console.log("player.load Failed: ",e);
                }
            };
        
            // Error event handler
            function errorEventHandler(evt)
            {
                console.log("Error Event ",evt.detail);    
            };
        
            // Destructor.  Unbind non-angular listeners
            scope.$on('$destroy',function()
            {
                player.removeEventListener('error');
            });
        }
    };

    return directive;
});

错误日志

[Log] tqVideoPlayer link. nodename – "TQ-VIDEO-PLAYER" – k [<tq-video-player id="my-video">]
[Log] mathRound.install (shaka-player.compiled.debug.js,line 1670)
[Log] MediaSource.install (shaka-player.compiled.debug.js,line 1675)
[Info] Patching Safari 11 & 12 MSE bugs. (shaka-player.compiled.debug.js,line 1675)
[Info] Using Apple-prefixed EME (shaka-player.compiled.debug.js,line 1685)
[Log] Pipwebkit.install (shaka-player.compiled.debug.js,line 1777)
[Log] VideoPlayPromise.install (shaka-player.compiled.debug.js,line 1783)
[Info] Using native VTTCue. (shaka-player.compiled.debug.js,line 1785)
[Log] MediaCapabilities: install (shaka-player.compiled.debug.js,line 1671)
[Debug] EmeEncryptionSchemepolyfill: Waiting to detect encryptionScheme support. (shaka-player.compiled.debug.js,line 1882)
[Debug] McEncryptionSchemepolyfill: Waiting to detect encryptionScheme support. (shaka-player.compiled.debug.js,line 1888)
[Log] Loading... – "https://dash.akamaized.net/akamai/bbb_30fps/bbb_30fps.mpd" (tqVideoPlayer.js,line 61)
[Log] loadplayer() (tqVideoPlayer.js,line 67)

[Warning] Factories requiring new has been deprecated and will be removed in v4.0 . We are currently at version v3.1 . Additional information: Factories should be plain functions (shaka-player.compiled.debug.js,line 181)

[Log] player.load Failed:  – TypeError: null is not an object (evaluating 'a.textTracks') — simple_text_displayer.js:37 (tqVideoPlayer.js,line 75)
TypeError: null is not an object (evaluating 'a.textTracks') — simple_text_displayer.js:37SimpleTextdisplayer — simple_text_displayer.js:37textdisplayFactory — player.js:4512callFactory — functional.js:110(anonymous function) — player.js:1494nextStep_ — generator_engine]:795next_ — generator_engine]:699next — generator_engine]:833(anonymous function) — execute_async_generator]:72initializePromisePromiseasyncExecutePromiseGenerator — execute_async_generator]:59asyncExecutePromiseGeneratorProgram — execute_async_generator]:117onInitializeMediaSourceEngine_ — player.js:1469(anonymous function) — player.js:563enterNode — player.js:602(anonymous function) — walker.js:342nextStep_ — generator_engine]:795next_ — generator_engine]:699next — generator_engine]:833(anonymous function) — execute_async_generator]:72initializePromisePromiseasyncExecutePromiseGenerator — execute_async_generator]:59asyncExecutePromiseGeneratorProgram — execute_async_generator]:117takeNextStep_ — walker.js:321doOneThing_ — walker.js:228(anonymous function) — walker.js:209nextStep_ — generator_engine]:795next_ — generator_engine]:699next — generator_engine]:833b — execute_async_generator]:52promiseReactionJob

[Error] Unhandled Promise Rejection: TypeError: this.target.addEventListener is not a function. (In 'this.target.addEventListener(b,c,this.options)','this.target.addEventListener' is undefined)
    Binding_ (shaka-player.compiled.debug.js:367:297)
    listen (shaka-player.compiled.debug.js:364:698)
    onAttach_ (shaka-player.compiled.debug.js:845:273)
    (anonymous function) (shaka-player.compiled.debug.js:824:356)
    enterNode (shaka-player.compiled.debug.js:826:404)
    (anonymous function) (shaka-player.compiled.debug.js:764:496)
    nextStep_ (shaka-player.compiled.debug.js:37:114)
    next_ (shaka-player.compiled.debug.js:33:244)
    next (shaka-player.compiled.debug.js:38)
    (anonymous function) (shaka-player.compiled.debug.js:39:235)
    initializePromise
    Promise
    asyncExecutePromiseGenerator (shaka-player.compiled.debug.js:39:135)
    asyncExecutePromiseGeneratorProgram (shaka-player.compiled.debug.js:39:448)
    takeNextStep_ (shaka-player.compiled.debug.js:764:123)
    doOneThing_ (shaka-player.compiled.debug.js:761:151)
    (anonymous function) (shaka-player.compiled.debug.js:760:193)
    nextStep_ (shaka-player.compiled.debug.js:37:114)
    next_ (shaka-player.compiled.debug.js:33:244)
    next (shaka-player.compiled.debug.js:38)
    b (shaka-player.compiled.debug.js:39)
    promiseReactionJob

在 MacOS 10.12 上的 Chrome、Safari 和 Firefox 上出现相同的故障。

AngularJS 1.6.9 沙卡播放器 3.1.0

解决方法

angular.element function 返回一个 jQuery 元素,jQuery find function 也是如此。因此,您的 videoTag 变量是指一个 jQuery 对象。

另一方面,shaka.Player constructor 需要一个 HTMLMediaElement。所以传递 videoTag[0] 应该有效。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...