Loop
This is no doubt one of the most used cases of music on any website (that actually uses music)…
-
var playr:Playr = new Playr();
-
playr.autoPlay=true;
-
playr.loadTrackFromURL("assets/music/loop.mp3");
-
playr.repeat = PlayrRepeat.REPEAT_SINGLE;
…and it was never easier.
Simple
This is as basic as it gets
-
var playr:Playr= new Playr("/assets/playlist.xml","/assets/music/",true);
When you execute this code, Playr will start playing the playlist described in the XML file at once. There is no way to do this any faster, or any simpeler. (Some people might not understand it so let me be clear about this: You 1) load a playlist 2) load the tracks 3) play the complete list 4) automaticlally whenever the music is loaded, using exactly 1 line of code. Shorter is not possible.)
Play a single track
In case you’re not fond of uploading all of your music.
-
var playr:Playr= new Playr();
-
playr.autoPlay=true;
-
playr.loadTrackFromURL('assets/music/myTrack.mp3');
Media player
Let’s add some buttons.
-
var playr:Playr= new Playr("/assets/playlist.xml","/assets/music/",true);
-
playr.registerPlayButton(btnPlay);
-
playr.registerPauseButton(btnPause);
-
playr.registerNextButton(btnNext);
-
playr.registerPreviousButton(btnPrevious);
-
playr.registerStopButton(btnStop);
Advanced media center
Let’s add error handling for all kinds of errors.
-
var playr:Playr= new Playr("/assets/playlist.xml","/assets/music/",true);
-
playr.registerPlayButton(btnPlay);
-
playr.registerPauseButton(btnPause);
-
playr.registerNextButton(btnNext);
-
playr.registerPreviousButton(btnPrevious);
-
playr.registerStopButton(btnStop);
-
playr.addEventListener(PlayrErrorEvent.PLAYLIST_STREAM_ERROR, playlistStreamErrorHandler);
-
playr.addEventListener(PlayrErrorEvent.SOUND_STREAM_ERROR, soundStreamErrorHandler);
-
playr.addEventListener(PlayrErrorEvent.PLAYLIST_INVALID_XML, playlistInvalidXMLErrorHandler);
-
playr.addEventListener(PlayrErrorEvent.TRACK_NOT_ADDED_TO_PLAYLIST, trackNotAddedToPlaylistErrorHandler);
All Playr errors are built to be silent. This means that not handling them will not result in error messages popping up on the screen of your visitor.
Playlist management
Let’s say you want to build an interactive music player, that allows you to build your own playlists. How to make the playlist pluggable?
-
var playr:Playr = new Playr();
-
var playlist:PlaylistManager = new PlaylistManager();
-
-
var track1:PlayrTrack = new PlayrTrack();
-
track1.artist = 'linkin park';
-
track1.album = 'minutes to midnight';
-
track1.title = 'what ive done';
-
track1.totalSeconds = 209;
-
track1.file = 'myTrack.mp3';
-
-
var track2:PlayrTrack = new PlayrTrack();
-
track2.artist = 'ICP';
-
track2.album = 'forgotten freshness';
-
track2.title = 'fly away';
-
track2.totalSeconds = 274;
-
track2.file = 'Fly Away.mp3';
-
-
var track3:PlayrTrack = new PlayrTrack();
-
track3.artist = 'gorki & abn';
-
track3.album = '-';
-
track3.title = 'ex-liefdadigheid';
-
track3.totalSeconds = 241;
-
track3.file = 'Gorki & ABN - Ex-liefdadigheid.mp3';
-
-
playlist.addTrack(track1);
-
playlist.addTrack(track2);
-
playlist.addTrack(track3);
-
-
playr.playlist = playlist;
Now imagine you create all kinds of playlists, and could let your users switch between them as they like it.
Edit the playlist
But what if you don’t like the playlist as is?
-
playr.playlist.moveTrackTo(5,2); //moves track #5 to the second place in the playlist
You can even do this kind of stuff after loading an existing XML file (for example an existing playlist saved on the server). This opens some doors, doesn’t it?