# SoundManager 2 Demo: "Page as a playlist"

## Muxtape.com-style UI, MP3/AAC Player Example

*CSS 3 border-radius supported only by Firefox 2.x+, Safari (2.x?) as of 04/2008 - perhaps IE 8 RTM..

This page uses shiny features which require Flash 9. The default config uses Flash 8.

- Crash cymbal
- Bass
- 440 Hz sine, square, sawtooth, pink noise, random
- 1Hz - 10KHz sweep
- 20060826 - Armstrong
- Barrlping with Carl (featureblend.com)
- Angry cow sound with "exclude" CSS class (Browser follows link normally)

## MPEG4 / H.264 + HE-AAC (Flash "MovieStar" 9.0r115+) format support - audio-only example

### A subset of MPEG4 is supported including AAC, FLV, MP4, M4A, MOV, MP4V, 3GP and 3G2 files.

MEPG4 video can be shown while playing these files, too. See the video demo.

- Rain on Car Roof (MPEG4 .AAC audio, 128 kbps AAC-LC exported from QuickTime)
- Jellyfish (848x480 H.264 video, playing audio portion only)

%s1 / %s2

### Basics

Don't want a Flash 9 requirement, EQ/waveform, perhaps less CPU? See Flash 8-based basic demo.

Clicking a title will start loading + playing, or pause, a sound.

Once loading, click (or click and drag) on the loading/position bar to seek within the sound.

The document title reflects the currently-playing sound, and by default the list will play sequentially if left alone. (This is configurable.)

### Themes

Just for fun, a few color schemes (visible when playing/paused):

- <ul class="playlist dark"> - #333 and #666, mostly
- <ul class="playlist gold"> - Frontin' the bling
- <ul class="playlist bubblegum"> - ZOMG PONIES!!!ONEONEONE
- <ul class="playlist"> (default)

Also add "shiny", eg. <ul class="shiny playlist">...

## Experimental (Alpha) Variant: MP3 "Metadata": Annotations / notes / sub-tracks

A potential approach to noting interesting moments in sounds, scene changes, new tracks in seamless DJ mixes etc. Keep in mind this is a single MP3 being loaded, but annotations are placed along the timeline as shown.

A "metadata" element contains a nested list of data (UL/LI combination) - in this case, a summary of each scene - and the time at which this item occurs/begins. In order to help with positioning, the total length of the sound is also specified up-front. View the source code of this page for the details.

- A Virtual Haircut (3 scenes)
  4:43

  - Electric razor

    0:00

  - Water, scissors

    2:41

  - More razor work

    4:00
- Rubber Chicken Launch (Office)
  0:47

  - First attempt

    0:00

  - Fire!

    0:02

  - "Too much angle"

    0:05

  - Random chicken noise

    0:18

  - "Wait a second"

    0:31

  - Derrr..

    0:34

  - Launch attempt #2

    0:36

  - "Wrong angle"

    0:39

  - "Fail"

    0:42

## How It Works

This example uses SoundManager 2 to find links to MP3 files within an unordered list, and makes them playable "in-place" on a page. The script assigns CSS classes to links' parent LI nodes to indicate their state (playing/paused, etc.)

### Progressive Enhancement

This provides a nice HTML base for all browsers and user agents, and enhances progressively for those with Javascript and Flash. Links pointing to MP3s are assigned an onclick handler which intercepts the click (preventing the browser from following the link and unloading the page. SM2 will then create sound objects as needed to play the MP3s. In the event there is an error at runtime or a lack of support (no JS/Flash etc.), the browser will simply load the MP3s directly as it normally would. This includes iPhones, etc.

### HTML Fragments (UI Element Templates)

Each item in the playlist has its own set of controls and progress indicators, etc. This content is defined once as a hidden template of HTML in-page separate from the playlist markup and is cloned for each item as needed. This can be easily styled with CSS as well, of course.

## I'd like to use this.

See this basic demo for reference.

The basic demo uses the default Flash 8 configuration, but you can easily change this to use Flash 9 features. The only difference in code is the configuration.

A reminder that if loading from the local filesystem, Flash will deny access to remote (network/internet) URLs by default unless whitelisted via the Flash Player Global Security Settings Page. Some URLs in this example are remote to demonstrate this.

## Configuration + Options

### Default configuration

Behaviours such as auto-start and UI elements like VU meters and spectrum graphs are easy configurable, using an object literal format as shown below.

The page player config (see related page-player.js file) as below.

The custom parameters used to make this demo page are highlighted in red.

      // ( within page-player.js )

      this.config = {
        flashVersion: 9,       // version of Flash to tell SoundManager to use - either 8 or 9.
        usePeakData: true,     // [Flash 9 only] show peak (VU-meter) data
        useFavIcon: true,      // try to show peakData in address bar (Firefox + Opera) - requires usePeakData = true too, of course.
        useWaveformData: true, // [Flash 9 only] true: show raw waveform data - WARNING: CPU-intensive
        useEQData: false,      // [Flash 9 only] show EQ (frequency spectrum) data - WARNING: CPU-intensive
        fillGraph: false,      // [Flash 9 only] draw full lines instead of only top (peak) spectrum points
        allowRightClick:true,  // let users right-click MP3 links ("save as...", etc.) or discourage (can't prevent.)
        useThrottling: false,  // try to rate-limit potentially-expensive calls (eg. dragging position around)
        autoStart: false,      // begin playing first sound when page loads
        playNext: true,        // stop after one sound, or play through list until end
        updatePageTitle: true, // change the page title while playing sounds
        emptyTime: '-:--'      // null/undefined timer values (before data is available)
      }

### Per-page configuration override

Alternately, you may override the defaults on a per-page basis by defining an "alternate configuration" object before the page-player.js file has been included in your source code:

      // ( before page-player.js )

      var PP_CONFIG = {
        flashVersion: 9,       // version of Flash to tell SoundManager to use - either 8 or 9. Flash 9 required for peak / spectrum data.
        usePeakData: true,     // [Flash 9 only] whether or not to show peak data (no notable CPU cost)
        useWaveformData: true  // [Flash 9 only] show raw waveform data. WARNING: Experimental, likely CPU-heavy
      }

Any options specified in PP_CONFIG will override the defaults defined in page-player.js.

### Basic CSS

     If you want to make your own UI from scratch, here is the base:

     ul.playlist {}

     Default + hover state, "click to play":

     li.sm2_link {}
     li.sm2_link:hover {}

     Playing + hover state, "click to pause":

     li.sm2_playing {}
     li.sm2_playing:hover {}

     Paused + hover state, "click to resume":

     li.sm2_paused {}
     li.sm2_paused:hover {}

The positioning (load status / control bar) template is also applied after each MP3 link, from an element named "control-template"

"loading" and "position" have background colors applied, and have their width adjusted dynamically by SM2 as the sound(s) load/play. "timing" is replaced with the current time / duration, eg. 1:23 / 4:20

The class names applied can be edited within the source JS also, for convenience.

The controls are shown and hidden via the same dynamic CSS updates. See the source CSS for the timing / status bar layout.

## Performance Considerations

### Experimental Flash 9 features

- #### Dynamic "favicon" VU meter

  The VU meter "favicon" option as shown in the address/location bar for Firefox and Opera can cause a lot of disk access in Firefox (2.x/3.0 at time of writing, from what has been reported.) It may be garbage collection-related.

  The behaviour seems to be connected to the dynamic swapping of <link> elements with data: URIs containing the VU meter data, and looks to be noticeable with the first sound played - after which point things settle down. Perhaps the browser is attempting to cache the favicon data being assigned.

- #### Waveform/spectrum visualization graph

  Enabling the waveformData and/or eqData features will result in some heavy DOM calls (manipulation of 256 <div> elements with each "frame" drawn) which can eat up a good amount of CPU and may make really old computers cower in fear.

  Ultimately, the UI refresh rate will simply be limited if a CPU ceiling is hit, and audio playback should not be affected.

SoundManager 2 project home