AngularJS应用程序中的JavaScript音频播放器插件加倍标记

时间:2015-08-18 16:11:42

标签: javascript angularjs html5-audio

我有一个Angular应用程序可以从数据库中提取许多音频文件。我正在使用Anthony Kolber的audio.js作为HTML5播放器,其中相关的包装器和html是从javascript生成的。问题是加价加倍,我无法弄清楚原因。它在ng-repeat内部或没有ng-repeat时都会这样做,并且在将宽度设置为加载和进度条时会导致连续的“未定义”错误。

javascript很长很难粘贴在这里。我使用代码here创建了问题的简化版本以及实时演示here

原始HTML:

<!-- audio links -->
<div ng-repeat="audio in medias | orderBy: 'track'" my-repeat-directive>
  <span><em>{{audio.title}}</em></span>
  <audio src="" dynamic-url dynamic-url-src="{{audio.url}}" preload="auto" />
</div>

渲染的HTM:

<div ng-repeat="audio in medias | orderBy: 'track'" my-repeat-directive="" class="ng-scope">
  <span><em class="ng-binding">Glass Jungle 1</em></span>

  <div class="audiojs loading" classname="audiojs" id="audiojs_wrapper0">

    <div class="audiojs " classname="audiojs" id="audiojs_wrapper3">

      <audio src="audio_files/Glass_Jungle_sample1.mp3" dynamic-url="" dynamic-url-src="Glass_Jungle_sample1.mp3" preload="auto"></audio>

      <div class="play-pause">
       <p class="play"></p>             
       <p class="pause"></p>             
       <p class="loading"></p>             
       <p class="error"></p>
      </div>
      <div class="scrubber">
        <div class="progress" style="width: 0%;"></div>
        <div class="loaded" style="width: 100%;"></div>
      </div>
      <div class="time">
        <em class="played">00:00</em>/<strong class="duration">01:25</strong>
      </div>
      <div class="error-message"></div>

    </div>

    <div class="play-pause">             
      <p class="play"></p>             
      <p class="pause"></p>             
      <p class="loading"></p>             
      <p class="error"></p>           
    </div>           
    <div class="scrubber">             
      <div class="progress"></div>             
      <div class="loaded"></div>           
    </div>           
    <div class="time">             
      <em class="played">00:00</em>/<strong class="duration">00:00</strong>           
    </div>           
    <div class="error-message"></div>

 </div>

1 个答案:

答案 0 :(得分:1)

我下载了你的代码并对其进行了简化,这对我有用:

&#13;
&#13;
<!-- audio links -->
<div ng-repeat="audio in medias | orderBy: 'track'">
  <span><em>{{audio.title}}</em></span>
  <audio src="" dynamic-url dynamic-url-src="{{audio.url}}" preload="auto" />
</div>

<!-- JavaScript -->
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- angularJS -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<!-- AngularJS Scripts -->
<script>
  var app = angular.module('myApp', []);
  app.controller('mainCtrl', function($scope, $rootScope) {
    $scope.medias = [{
      "track": 1,
      "title": "Glass Jungle 1",
      "url": "Glass_Jungle_sample1.mp3"
    }, {
      "track": 2,
      "title": "Glass Jungle 2",
      "url": "Glass_Jungle_sample2.mp3"
    }, {
      "track": 3,
      "title": "Glass Jungle 3",
      "url": "Glass_Jungle_sample3.mp3"
    }];

    audiojs.events.ready(function() {
      var as = audiojs.createAll();
    });

  });
  app.directive('dynamicUrl', function() {
    return {
      restrict: 'A',
      link: function postLink(scope, element, attr) {
        element.attr('src', 'audio_files/' + attr.dynamicUrlSrc);
      }
    };
  });
</script>
&#13;
&#13;
&#13;

其余的都是一样的。我没有深入研究为什么你的代码出现问题的原因,而且自从我使用angular以来已经有一段时间了,但我认为它与myRepeatDirective有关,它执行了3次,每个音频文件一次。我猜测标记只复制了两次,因为第二次通过它遇到了javascript错误,以防止它第三次执行。