1 window.SM2_DEFER = true; 2 define(function () { 3 4 // Give each player a unique ID number 5 var _id = 0; 6 var getId = function () { 7 return ++_id; 8 }; 9 10 /** 11 * A single track player compatible with Transistor and Last.fm 12 * Emits events through amplify.js message bus 13 */ 14 var Player = Class.extend({ 15 init: function(swfUrl) { 16 this.id = getId(); 17 window.soundManager = new SoundManager(swfUrl); // Flash expects window.soundManager. 18 soundManager.beginDelayedInit(); 19 this.playlist = null; 20 this.volume = 100; 21 }, 22 23 play: function (track) { 24 25 if (track) { 26 if (this.audio && this.audio.playState === 1) { 27 this.audio.unload(); 28 } 29 var self = this; 30 var audio = soundManager.createSound({ 31 id: "track" + track.streamid, 32 url: track.location, 33 autoLoad: true, 34 autoPlay: true, 35 multishop: false, 36 volume: this.volume, 37 onfinish: function () { 38 amplify.publish('transistorplayer:finished', self.id, track); 39 }, 40 onstop: function () { 41 amplify.publish('transistorplayer:stopped', self.id, track); 42 }, 43 onpause: function () { 44 amplify.publish('transistorplayer:paused', self.id, track); 45 }, 46 onresume: function () { 47 amplify.publish('transistorplayer:resumed', self.id, track); 48 }, 49 onplay: function () { 50 amplify.publish('transistorplayer:playing', self.id, track); 51 }, 52 onsuspend: function () { 53 amplify.publish('transistorplayer:suspended', self.id, track); 54 }, 55 whileplaying: function () { 56 amplify.publish('transistorplayer:whileplayling', self.id, track, { 57 position: this.position, 58 duration: this.duration 59 }); 60 } 61 }); 62 this.audio = audio; 63 } else if (this.hasTrack()) { 64 this.audio.resume(); 65 } 66 }, 67 pause: function () { 68 this.audio.pause(); 69 }, 70 setVolume: function (vol) { 71 this.volume = vol; 72 if (this.audio) { 73 soundManager.setVolume(this.audio.sID, vol); 74 } 75 }, 76 onSMReady: function () { 77 this.ready = _soundManagerReady; 78 amplify.publish('transistorplayer:ready'); 79 }, 80 hasTrack: function () { 81 return this.audio && this.audio.playState === 1; 82 } 83 84 }); 85 86 return Player; 87 88 }); 89