Skip to content

Instantly share code, notes, and snippets.

@tamebadger
Created April 26, 2016 02:32
Show Gist options
  • Save tamebadger/e7ba3fe359628ceb1f5d169307fb07d5 to your computer and use it in GitHub Desktop.
Save tamebadger/e7ba3fe359628ceb1f5d169307fb07d5 to your computer and use it in GitHub Desktop.
Audio Player
import Ember from 'ember';
export default Ember.Component.extend({
audioPlayer: Ember.inject.service('audio-player'),
actions: {
start(){
this.get('audioPlayer').start()
},
stop(){
this.get('audioPlayer').stop()
},
next(){
this.get('audioPlayer').nextSong()
},
previous(){
this.get('audioPlayer').previousSong()
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
});
import Ember from 'ember';
export default Ember.Service.extend({
ids: [0,1,2,3,4,5,6,7,8,9,10],
songs: Ember.computed('ids',function(){
return this.get('ids').map(id => {
return { id: id, title: `Awesome Song ${id}`}
})
}),
currentlyPlaying: '',
currentIndex: 0,
currentStatus: 'stopped',
start(){
this.setSongByIndex()
this.set('currentStatus','playing')
},
stop(){
this.set('currentStatus','stopped')
},
nextSong(){
const maxIndex = this.get('ids.length') - 1
const currentIndex = this.get('currentIndex')
const nextIndex = currentIndex + 1
if(nextIndex > maxIndex){
this.stop()
}else{
this.set('currentIndex',nextIndex)
this.setSongByIndex()
}
},
previousSong(){
const maxIndex = this.get('ids.length') - 1
const currentIndex = this.get('currentIndex')
const prevIndex = currentIndex - 1
if(prevIndex < 0){
this.stop()
}else{
this.set('currentIndex',prevIndex)
this.setSongByIndex()
}
},
setSongByIndex(){
const songs = this.get('songs')
const currentIndex = this.get('currentIndex')
this.set('currentlyPlaying',songs[currentIndex])
}
});
Song Title: {{audioPlayer.currentlyPlaying.title}} <br/>
Audio Player Status: {{audioPlayer.currentStatus}} <br/>
<button {{action 'start'}}>Start</button> |
<button {{action 'next'}}>Next</button> |
<button {{action 'previous'}}>Previous</button> |
<button {{action 'stop'}}>Stop</button> |
{
"version": "0.7.2",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.4/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.4.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.4/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment