mhatch
mhatch

Reputation: 4605

JavaScript audio player plugin doubling markup in AngularJS app

I have an Angular app that pulls in many audio files from a database. I'm using audio.js by Anthony Kolber as the HTML5 player, where the associated wrapper and html are generated from the javascript. The problem is the markup is doubling and I can't figure out why. It does so both within or without an ng-repeat and causes a continuous 'undefined' error when setting width to the loading and progress bars.

The javascript is quite long and difficult to paste here. I've created a simplified version of the problem with the code here as well as a live demo here.

The original 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>

The rendered 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>

Upvotes: 0

Views: 372

Answers (1)

efeder
efeder

Reputation: 562

I downloaded your code and simplified it a bit and it worked for me:

<!-- 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>

The rest is the same. I didn't delve too deeply into the whys of why your code was having problems, and it's been a while since I worked with angular, but I think it had to do with myRepeatDirective, which is executed 3 times, once for each audio file. I'm guessing the markup was only duplicated twice because the second time through it encountered the javascript errors to prevent it from executing a third time.

Upvotes: 1

Related Questions