Bright Hub
 
Matthew Casperson's Hubfolio

Add a sound visualizer to Adobe Flex

Article by Matthew Casperson (227,413 pts )
Published on Nov 11, 2009

This free tutorial shows you how to add a sound visualizer to any Flex application.

Introduction

VIEW THE DEMO

DOWNLOAD THE SOURCE CODE

The article Create an MP3 player with Adobe Flex demonstrated how a simple MP3 player could be created with Adobe Flex. It had the basic controls you would expect a music player to have, but one thing it was missing was some sort of spectrum analyser.

This tutorial will show you how to use the free Visualization control from Ben Stucki to add a visual spectrum analyser.

Adobe Flex Visualization Screenshot

Step 1 - Download the Visualization library

Download the Visualization library from here. Extract it and then add the folder to the Flex Build Path.

Adobe Flex Visualization Screenshot

Step 2 - Create the Visualization control

Add the following attribute to the Flex Application element.

xmlns:fx="com.fusiox.ui.*"

This sets the XML namespace for the Visualization control. Now add the following element as a child of the Application element.

<fx:Visualization click="changeVisType()" id="visualisation" height="50" width="166" horizontalCenter="0" verticalCenter="-25"/>

This createsthe Visualization control. We have also set the changeVisType function to be called when the Visualization is clicked.

Step 3 - Define the variables

The Visualization has three display modes: line, wave and bars. By clicking on the Visualization, the user can chage the display type. To enable this functionality we need to create an array that holds the display types (defined as strings), and an integer that points to the current display type in the Array.

private var types:Array = new Array("line", "wave", "bars");

private var selectedType:int = 1;

Step 4 - Initialise the Visualization

The default Visualization display type is line, which is pretty boring. In the entry point function (appComplete in this case) we set the initial Visualization display type to wave (notice that this corresponds to the initial value of the selectedType variable in step 3). We also set the Visualization fill colour to something that matches our MP3 player colour scheme.

this.visualisation.setStyle("audioFillColor", 0x009dff);

this.visualisation.type = "wave";

Step 5 - Changing the display type

The changeVisType function is called when the Visualization has been clicked. Here we simply increment the selectedType variable, and change the Visualization type property to the string held at the new point in the Array.

private function changeVisType():void

{

selectedType = (selectedType + 1) % this.types.length;

this.visualisation.type = this.types[selectedType];

}

Conclusion

The Visualization control accesses the sounds being played via the SoundMixer.computeSpectrum function. This means that it will display the spectrum of all sounds being played, and you don't have to specifically link the sounds you have loaded and played with the Visualization control. In this way the Visualization control can be dropped into any Flex application with almost no modification of the existing code required.

If you get an exception saying "A policy file is required, but the checkPolicyFile flag was not set when this media was loaded", you can set checkPolicyFile to true when loading a sound using the code below.

sound.load(new URLRequest(MP3_URL), new SoundLoaderContext(1000, true));

Return to the Tutorial Index

Search More About:

Comments

Feb 14, 2010 10:57 AM
Andrey
Problem
The problem is the following: when i use URL with mp3 file it works great, but when i use something like web radio stream URL it doesn't show visualizations. How to skip such problem
Feb 14, 2010 10:30 AM
Andrey
Problems
Hi, i have one problem with this code - it works well on my local machine but there arent any visualization when i've uploaded it on my production server. What can be a reason of such unexpected behaviour
Nov 17, 2009 2:25 AM
Soumyabrata Paul
This is Great
Hi Matthew, This is great work. Very nice. I must say most simple also.
 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2010 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape