Playr

Playr. Hands down the easiest way to play music in Flash Actionscript 3.

Frontpage news

Demos of how it is done

Demos

How to use the Playr class

Tutorials
Download

Howto

Loop

This is no doubt one of the most used cases of music on any website (that actually uses music)…

import com.nocreativity.playr.*;
  1. var playr:Playr = new Playr();
  2. playr.autoPlay=true;
  3. playr.loadTrackFromURL("assets/music/loop.mp3");
  4. playr.repeat = PlayrRepeat.REPEAT_SINGLE;

…and it was never easier.

Simple

This is as basic as it gets

import com.nocreativity.playr.Playr;
  1. 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.

import com.nocreativity.playr.Playr;
  1. var playr:Playr= new Playr();
  2. playr.autoPlay=true;
  3. playr.loadTrackFromURL('assets/music/myTrack.mp3');

Media player

Let’s add some buttons.

import com.nocreativity.playr.*;
  1. var playr:Playr= new Playr("/assets/playlist.xml","/assets/music/",true);
  2. playr.registerPlayButton(btnPlay);
  3. playr.registerPauseButton(btnPause);
  4. playr.registerNextButton(btnNext);
  5. playr.registerPreviousButton(btnPrevious);
  6. playr.registerStopButton(btnStop);

Advanced media center

Let’s add error handling for all kinds of errors.

import com.nocreativity.playr.*;
  1. var playr:Playr= new Playr("/assets/playlist.xml","/assets/music/",true);
  2. playr.registerPlayButton(btnPlay);
  3. playr.registerPauseButton(btnPause);
  4. playr.registerNextButton(btnNext);
  5. playr.registerPreviousButton(btnPrevious);
  6. playr.registerStopButton(btnStop);
  7. playr.addEventListener(PlayrErrorEvent.PLAYLIST_STREAM_ERROR, playlistStreamErrorHandler);
  8. playr.addEventListener(PlayrErrorEvent.SOUND_STREAM_ERROR, soundStreamErrorHandler);
  9. playr.addEventListener(PlayrErrorEvent.PLAYLIST_INVALID_XML, playlistInvalidXMLErrorHandler);
  10. 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?

import com.nocreativity.playr.*;
  1. var playr:Playr = new Playr();
  2. var playlist:PlaylistManager = new PlaylistManager();
  3.  
  4. var track1:PlayrTrack = new PlayrTrack();
  5. track1.artist = 'linkin park';
  6. track1.album = 'minutes to midnight';
  7. track1.title = 'what ive done';
  8. track1.totalSeconds = 209;
  9. track1.file = 'myTrack.mp3';
  10.  
  11. var track2:PlayrTrack = new PlayrTrack();
  12. track2.artist = 'ICP';
  13. track2.album = 'forgotten freshness';
  14. track2.title = 'fly away';
  15. track2.totalSeconds = 274;
  16. track2.file = 'Fly Away.mp3';
  17.  
  18. var track3:PlayrTrack = new PlayrTrack();
  19. track3.artist = 'gorki & abn';
  20. track3.album = '-';
  21. track3.title = 'ex-liefdadigheid';
  22. track3.totalSeconds = 241;
  23. track3.file = 'Gorki & ABN - Ex-liefdadigheid.mp3';
  24.  
  25. playlist.addTrack(track1);
  26. playlist.addTrack(track2);
  27. playlist.addTrack(track3);
  28.  
  29. 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.removeTrack(0);
  1. 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?