Skip to content

Commit

Permalink
Merge pull request #34 from nypublicradio/feature/new-event-and-metadata
Browse files Browse the repository at this point in the history
Add service-level change event and sound metadata
  • Loading branch information
jkeen committed Apr 19, 2017
2 parents a921d38 + e59e582 commit 0d98754
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
15 changes: 13 additions & 2 deletions addon/services/hifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,23 @@ export default Service.extend(Ember.Evented, DebugLogging, {
});
});


promise.then(({sound}) => sound.set('metadata', options.metadata));
promise.then(({sound}) => this.get('soundCache').cache(sound));


// On audio-played this pauses all the other sounds. One at a time!
promise.then(({sound}) => this.get('oneAtATime').register(sound));
promise.then(({sound}) => sound.on('audio-played', () => this.setCurrentSound(sound)));

promise.then(({sound}) => sound.on('audio-played', () => {
let previousSound = this.get('currentSound');
let currentSound = sound;

if (previousSound !== currentSound) {
this.trigger('current-sound-changed', {previousSound, currentSound});
this.setCurrentSound(sound);
}
}));

return promise;
},
Expand Down Expand Up @@ -548,7 +560,6 @@ export default Service.extend(Ember.Evented, DebugLogging, {
let Connection = strategy.connection;
let connectionOptions = getProperties(strategy, 'url', 'connectionName', 'sharedAudioAccess');
let sound = Connection.create(connectionOptions);

this.debug('ember-hifi', `TRYING: [${strategy.connectionName}] -> ${strategy.url}`);

sound.one('audio-load-error', (error) => {
Expand Down
49 changes: 48 additions & 1 deletion tests/unit/services/hifi-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,56 @@ test("service has access to the current sound inside the play callback", functio
let connections = ['NativeAudio'];
let service = this.subject({ options: chooseActiveConnections(...connections) });
let s1url = "/assets/silence.mp3";

return service.play(s1url).then(({sound}) => {
assert.equal(sound.get('position'), service.get('position'));
done();
});
});

test("service triggers `current-sound-changed` event when sounds change", function(assert) {
let done = assert.async();
let connections = ['NativeAudio'];
let service = this.subject({ options: chooseActiveConnections(...connections) });
let s1url = "/assets/silence.mp3";
let s2url = "/assets/silence2.mp3";

assert.expect(4);

service.one('current-sound-changed', ({previousSound, currentSound}) => {
assert.equal(previousSound, undefined, "there should not a previous sound");
assert.equal(currentSound.get('url'), s1url, "current sound should be the first sound");
});

return service.play(s1url).then(() => {
service.one('current-sound-changed', ({previousSound, currentSound}) => {
assert.equal(previousSound.get('url'), "/assets/silence.mp3", "previous sound should be this sound");
assert.equal(currentSound.get('url'), "/assets/silence2.mp3");
});
return service.play(s2url).then(() => done());
});
});


test("metadata can be sent with a play and load request and it will stay with the sound", function(assert) {
let done = assert.async();
let connections = ['NativeAudio'];
let service = this.subject({ options: chooseActiveConnections(...connections) });
let s1url = "/assets/silence.mp3";
let s2url = "/assets/silence2.mp3";

let storyId = 12544;
let currentSound;

return service.play(s1url, {metadata: {
storyId: storyId
}}).then(({sound}) => {
assert.equal(sound.get('metadata.storyId'), storyId, "storyId should be in metadata");
currentSound = sound;
return service.play(s2url).then(({sound}) => {
assert.equal(sound.get('metadata.storyId'), undefined, "metadata hasn't been set and shouldn't exist");
assert.equal(currentSound.get('metadata.storyId'), storyId, "storyId should be in saved sound");
done();
});
});
});

0 comments on commit 0d98754

Please sign in to comment.