1

I am having an issue getting the HTMLAudioElement to work in my angular.js application. I am using a controller to push the function to my play button but the click is unresponsive. I have the function inside of the ng-click directive as playpause() but it still is not responsive. I have no errors when my code runs. Any ideas?

Here is my controller code:

angular.module('MainCtrl', []).controller('MainController', function($scope) {

$scope.audio = new Audio('../../data/mp3/pound.mp3');
            $scope.currentNum = 0;

            // tell audio element to play/pause, you can use $scope.audio.play()
            $scope.playpause = function(){return $scope.audio.play();};
   });

and here is my html:

 <!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="/">

    <title>Starter Node and Angular</title>

    <!-- CSS -->
    <link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="libs\font-awesome\css\font-awesome.min.css"> 

    <!-- JS -->
    <script src="libs/angular/angular.min.js"></script>
    <script src="libs/angular-route/angular-route.min.js"></script>

    <!-- ANGULAR CUSTOM -->
    <script src="js/controllers/MainCtrl.js"></script>
    <script src="js/controllers/NerdCtrl.js"></script>
    <script src="js/services/NerdService.js"></script>
    <script src="js/appRoutes.js"></script>
    <script src="js/app.js"></script>
</head>
    <body ng-app="sampleApp" ng-controller="MainController">

     <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Moozik</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
</div>
    </nav>

    <!-- /.container -->


            <div class="container-fluid player">
            <span id="play" ng-click="playpause()" class="fa fa-play fa-4x"></span>&nbsp;
            <span id="pause" class="fa fa-pause fa-4x"></span>&nbsp;
            <span id="stop" class="fa fa-stop fa-4x"></span>
            <span id="next" class="fa fa-fast-forward fa-4x pull-right"></span>
            <span class="marquee pull-right col-xs-8">Hello</span>

           </div>


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>


  </body>
</html>
1
  • 1
    If you want to execute $scope.audio.play you could do this<span id="play" ng-click="audio.play()" class="fa fa-play fa-4x"></span> Commented Jan 23, 2015 at 20:59

1 Answer 1

1

I strongly not recommend to put an audio file into the $scope. Just store the audio reference in your directive like:

var myAudio = new Audio('../../data/mp3/pound.mp3');
$scope.play = function(){
  myAudio.play();
};
Sign up to request clarification or add additional context in comments.

3 Comments

I wrote "play" not "playpause", because audio.play is not a toggle function, so you need to write your own pause for this. also I have to mention that, you should make a directive for this, then pass in, which file the directive should play
I noticed you changed the name. Added ng-click="play()" and it still does not work. Am I missing something here? I do plan to move to a directive but I wanted to get simple functionality working at least.
This works so there must be a problem with where I am pointing to the media file! Thanks a bunch sir!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.