Skip to content

Commit 5bb2e3c

Browse files
authored
Merge branch 'master' into master
2 parents da27220 + c81f1e3 commit 5bb2e3c

6 files changed

+84
-35
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ Total awesomeness for playing sounds. Project page here:
2222
##[Angular Audio Documentation](http://danielstern.github.io/ngAudio/#/docs)
2323

2424

25-
Release Notes v1.7.3
25+
Release Notes v1.7.4
2626

2727
- Updated Angular dependency to support 1.6.x
2828

29+
Release Notes v1.7.3
30+
31+
- Add toFinish callback
2932

3033
Release Notes v1.7.2
3134

app/angular.audio.js

+63-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
'use strict';
22
angular.module('ngAudio', [])
3+
.constant('ngAudioDomUid', (function () {
4+
var domUid = '';
5+
for (var i=0; i<8; i++)
6+
{
7+
domUid = domUid + Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
8+
}
9+
return domUid;//unique id persisting through module life
10+
})())
311
.directive('ngAudio', ['$compile', '$q', 'ngAudio', function($compile, $q, ngAudio) {
412
return {
513
restrict: 'AEC',
@@ -100,24 +108,33 @@ angular.module('ngAudio', [])
100108
};
101109
}])
102110

103-
.service('remoteAudioFindingService', ['$q', function($q) {
111+
.service('remoteAudioFindingService', ['$q', 'ngAudioDomUid', function($q, ngAudioDomUid) {
104112

105113
this.find = function(url) {
106114
var deferred = $q.defer();
107-
var audio = new Audio();
108-
109-
audio.addEventListener('error', function() {
110-
deferred.reject();
111-
});
112-
113-
audio.addEventListener('loadstart', function() {
114-
deferred.resolve(audio);
115-
});
115+
var $sound = document.getElementById(ngAudioDomUid);
116+
if (!$sound)
117+
{
118+
var audioTag = document.createElement('audio');
119+
audioTag.style.display = 'none';
120+
audioTag.id = ngAudioDomUid;
121+
audioTag.src = url;
122+
document.body.appendChild(audioTag);
123+
$sound = document.getElementById(ngAudioDomUid);
124+
$sound.load();
125+
}
126+
else
127+
{
128+
$sound.pause();
129+
$sound.src = url;
130+
$sound.load();
131+
}
116132

117-
// bugfix for chrome...
118-
setTimeout(function() {
119-
audio.src = url;
120-
}, 1);
133+
if ($sound) {
134+
deferred.resolve($sound);
135+
} else {
136+
deferred.reject(id);
137+
}
121138

122139
return deferred.promise;
123140

@@ -151,8 +168,10 @@ angular.module('ngAudio', [])
151168
return function(id, scope) {
152169

153170
function twiddle(){
154-
audio.play();
155-
audio.pause();
171+
try{
172+
audio.play();
173+
audio.pause();
174+
}catch(e){}
156175
window.removeEventListener("click",twiddle);
157176
}
158177

@@ -188,7 +207,12 @@ angular.module('ngAudio', [])
188207
var completeListeners = [];
189208
this.complete = function(callback){
190209
completeListeners.push(callback);
191-
}
210+
};
211+
212+
var toFinishListeners = [];
213+
this.toFinish = function(secs, callback){
214+
toFinishListeners.push({'secs': secs, 'callback': callback});
215+
};
192216

193217
this.pause = function() {
194218
$willPause = true;
@@ -248,6 +272,10 @@ angular.module('ngAudio', [])
248272
}
249273
}
250274

275+
this.destroyed = function() {
276+
return $destroyed;
277+
};
278+
251279
function $setWatch() {
252280
if ($destroyed) {
253281
return;
@@ -298,6 +326,10 @@ angular.module('ngAudio', [])
298326
}, true);
299327
}
300328

329+
function audioLoadError() {
330+
audioObject.error = true;
331+
}
332+
301333
cleverAudioFindingService.find(id)
302334
.then(function(nativeAudio) {
303335
audio = nativeAudio;
@@ -311,14 +343,13 @@ angular.module('ngAudio', [])
311343

312344
}
313345

346+
audio.addEventListener('error', audioLoadError);
347+
314348
audio.addEventListener('canplay', function() {
315349
audioObject.canPlay = true;
316350
});
317351

318-
}, function(error) {
319-
audioObject.error = true;
320-
console.warn(error);
321-
});
352+
}, audioLoadError);
322353

323354

324355
var interval = $interval(checkWatchers, ngAudioGlobals.performance);
@@ -327,7 +358,7 @@ angular.module('ngAudio', [])
327358
},function(){
328359
$interval.cancel(interval);
329360
interval = $interval(checkWatchers, ngAudioGlobals.performance);
330-
})
361+
});
331362

332363
function checkWatchers() {
333364
if ($audioWatch) {
@@ -347,7 +378,8 @@ angular.module('ngAudio', [])
347378
}
348379

349380
if ($willRestart) {
350-
audio.src = 'about:blank';
381+
audio.pause();
382+
audio.currentTime = 0;
351383
$willRestart = false;
352384
}
353385

@@ -375,7 +407,7 @@ angular.module('ngAudio', [])
375407
audioObject.src = audio.src;
376408

377409
//After we check if progress is bigger than 0, and we set
378-
var tempProgress = (audio.currentTime / audio.duration);
410+
var tempProgress = (audio.currentTime / audio.duration).toPrecision();
379411
if(tempProgress > 0 ){
380412
audioObject.progress = tempProgress;
381413
}
@@ -386,6 +418,13 @@ angular.module('ngAudio', [])
386418
})
387419
}
388420

421+
toFinishListeners.forEach(function(listener) {
422+
if ((audioObject.duration - audioObject.currentTime) <= listener.secs) {
423+
listener.callback(audioObject);
424+
toFinishListeners.shift();
425+
}
426+
});
427+
389428
if ($looping && audioObject.currentTime >= audioObject.duration) {
390429
if ($looping !== true) {
391430
$looping--;

app/partial/audioEditView.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ <h3>
1616
<button class='btn btn-primary' ng-click='audio.paused ? audio.play() : audio.pause()'>{{audio.paused ? "Play" : "Pause" }}</button>
1717
<button class='btn btn-danger' ng-click='audio.restart()'>Stop</button>
1818
<button class='btn btn-neutral' ng-click='audio.muting = !audio.muting'>{{audio.muting ? "Unmute" : "Mute" }}</button>
19+
<button class='btn btn-neutral' ng-click='audio.destroy()'>{{audio.destroyed() ? "Destroyed" : "Destroy" }}</button>
1920
<!-- <div class='checkbox pull-right'>
2021
<label>
2122
<input type=checkbox ng-model=audio.loop ng-click='audio.loop = !audio.loop'>Loop
@@ -27,13 +28,13 @@ <h3>
2728
<div class="form-group col-xs-3">
2829
<label>Loop
2930
<select class='form-control input-small select' ng-model=audio.loop ng-options="value for value in [true,0,1,2,3,4,5]"></select>
30-
</label>
31+
</label>
3132
</div>
3233

3334
<!-- <label> -->
3435
<!-- Loop -->
3536
<!-- </label> -->
36-
37+
3738

3839
</form>
3940
</div>

app/partial/ngAudioDocs.html

+11-3
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ <h4>NgAudioObject Reference</h4>
9494
<tr>
9595
<td>unbind()</td>
9696
<td><p>
97-
98-
Removes all the listeners from the audio object, improving performance, but disabling most read functionality.
97+
98+
Removes all the listeners from the audio object, improving performance, but disabling most read functionality.
9999
</p>
100100
<p>
101101
For example, setting progress and currentTime will still work, but reading them will not return the correct value.
@@ -190,6 +190,14 @@ <h4>NgAudioObject Reference</h4>
190190
</p>
191191
</td>
192192
</tr>
193+
<tr>
194+
<td>destroyed:boolean (read only)</td>
195+
<td>
196+
<p>
197+
Is true if the NgAudioObject has been destroyed.
198+
</p>
199+
</td>
200+
</tr>
193201
</table>
194202
</article>
195203
<article>
@@ -283,7 +291,7 @@ <h5>Example Usage</h5>
283291
<code>
284292
<pre ng-non-bindable>
285293
&lt;div&gt;Current Time: &#123;&#123;audio.currentTime | trackTime&#125;&#125;&lt;/div&gt; </pre></code>
286-
294+
287295
<h5>Example Table</h5>
288296
<table class="table table-striped table-bordered table-condensed">
289297
<thead>

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-audio",
3-
"version": "1.7.2",
3+
"version": "1.7.3",
44
"description": "Total awesomeness for playing sounds in AngularJS",
55
"license": "MIT",
66
"main": "app/angular.audio.js",

package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
"ngaudio",
2020
"angular-audio"
2121
],
22-
"dependencies": {
23-
"browser-sync": "^2.7.12",
24-
"gulp": "^3.9.0"
25-
},
2622
"devDependencies": {
23+
"browser-sync": "^2.14.0",
24+
"gulp": "^3.9.1",
2725
"gulp-gh-pages": "^0.5.2"
2826
},
2927
"engines": {

0 commit comments

Comments
 (0)