简体   繁体   中英

customizing html5 audio tag, how to add a volume button?

I just create a directive in order to set up a live streaming based on the HTML5 tag, but I need to add a volume button, how can I implement it ?

Look at the directive

var app = angular.module('app', []);

app.directive('audioPlay', function() {
  return {
    restrict: 'E',
    link: function(scope, element, attr) {

      scope.playOrPause = true;

      var player = element.children('.player')[0];

      scope.playMusic = function() {
        scope.playOrPause = false;
        player.play();
      }

      scope.stopMusic = function() {
        scope.playOrPause = true;
        player.pause();
      }

    }
  };
});

and here the corresponding html

<div ng-app='app'>
      <audio-play>
        <audio class="player">
          <source src="http://fire.wavestreamer.com:9711/UrbanetRadio"/>
        </audio>
        <button class="button button-clear"
                ng-click="playMusic()"
                ng-hide="!playOrPause">
          PLAY
        </button>
        <button class="button button-clear"
                ng-click="stopMusic()"
                ng-show="!playOrPause">
          PAUSE
        </button>
      </audio-play>
</div>

I don't want to implement the basic html5 audio with the controls attr, I am doing this by myself and all I need is some help with volume button.

Here is some code that might help. You can use input type="range" as a volume control:

<input type="range" ng-model="volume" ng-change="changeVolume($event)" step="0.1" min="0" max="1">

And change sound level using volume property of the audio element:

scope.changeVolume = function(event) {
    player.volume = scope.volume; // from 0 to 1
};

You can also bind input's value to a model, or use buttons instead of a slider - there are many variations but you got the idea :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM