HTML5 Video is not working with AngularJS ng-src tag

后端 未结 10 1121
北恋
北恋 2020-11-29 02:25

AngularJS ng-src doesn\'t work with HTML5 Video element in this fiddle: http://jsfiddle.net/FsHah/5/

Looking at video element, the src tag is being popu

相关标签:
10条回答
  • 2020-11-29 02:38

    I think what's happening is Angular fills in the src attributes after the and elements have been added to the page, so the browser sees the elements with broken URLs. I worked around it using ng-if:

            <video muted ng-if="foo" loop autoplay>
                <source ng-src="{{foo.media.mp4.url}}">
                <source ng-src="{{foo.media.webm.url}}" type="video/webm">
                <img ng-src="{{foo.media.gif.url}}">
            </video>
    

    This makes the element tied to the existence of foo, which is a scope variable being populated via AJAX call.

    0 讨论(0)
  • 2020-11-29 02:47

    workaround

    in controller

    $scope.mp3 = "http://download.jw.org/audio.mp3"
    
    $scope.$watch('mp3', function() {
           $("audio").attr("src",$scope.mp3)
       });
    

    html:

    <audio id="mejs" type="audio/mp3" controls="controls"></audio>
    
    0 讨论(0)
  • 2020-11-29 02:47

    Just use vanilla js (regular javascript) to make this work. If you are listening to events like onended you might want to reconsider using $scope.$apply().

    My example:

    document.getElementById('video').src = $scope.videos[$scope.videoindex];
    
    0 讨论(0)
  • 2020-11-29 02:50

    Remove source tag from video tag and try this..

    <video controls preload=auto ng-src="{{videoURL| trustUrl}}" poster="{{thumbnailUrl}}"></video>
    

    and in your angular app create a filter like this

     app.filter("trustUrl", function($sce) {
                return function(Url) {
                    console.log(Url);
                    return $sce.trustAsResourceUrl(Url);
                };
            });
    
    0 讨论(0)
提交回复
热议问题