Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service-level change event and sound metadata #34

Merged
merged 4 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});