Skip to content

Commit

Permalink
stops streams by setting src attribute to the empty string (#9)
Browse files Browse the repository at this point in the history
rather than removing it. for whatever reason browsers seem to respond to
an empty string better than an removed attribute.

closes #7
  • Loading branch information
Brian Whitton authored and jkeen committed Sep 25, 2016
1 parent 181f7a2 commit 18e9e79
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion addon/hifi-connections/native-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ let Sound = BaseSound.extend({
stop() {
let audio = this.get('audio');
audio.pause();
audio.removeAttribute('src');
// must explicitly set it to the empty string or else the browser will
// continue to download
audio.setAttribute('src', '');
},

willDestroy() {
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/hifi-connections/native-audio-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ test("If it's a stream, we stop on pause", function(assert) {
assert.equal(sound.get('audio').src, goodUrl, "audio src attribute is set");

sound.stop();
assert.equal(sound.get('audio').src, "", "audio src attribute is not set");
// audio elements need their src attribute explicitly set to the empty string
// to stop downloading a stream, i.e. setAttribute('src', '').
// an attribute cleared with removeAttribute will return null when accessed
// with getAttribute so we use the DOM api here to verify that it's the
// empty string.
assert.equal(sound.get('audio').getAttribute('src'), "", "audio src attribute is set to the empty string");
assert.equal(stopSpy.callCount, 1, "stop was called");
});

0 comments on commit 18e9e79

Please sign in to comment.