Working With MP3 Files > Playing MP3 files

 

Playing MP3 files

The default play-back format for Macromedia Flash files is FLV. In your applications, you can also use Client-Side Communication ActionScript to play back MP3 audio files and the ID3 tags of MP3 files, and use Server-Side Communication ActionScript to publish MP3 files over a stream.

To do so, upload the MP3 files for your application to the /streams/application_instance subdirectory in your registered application directory. (Macromedia Flash Communication Server MX 1.5 creates a /streams subdirectory when you record a stream; if the /streams subdirectory doesn't exist, you can create it manually.) For example, if you have an application named CDPlayerApp in your Flash Communication Server applications directory, you could upload the MP3 files for that application to /applications/CDPlayerApp/streams/application_instance.

You can share MP3 files across all instances of an application by placing the shared MP3 files into a directory and specifying the location of this shared directory as a virtual directory in the <Streams> tag in the Vhost.xml file for that application. Then, in your Stream.play statement, specify the virtual directory and the MP3 file you want to play. For information on virtual directories and the <Streams> tag, see The Vhost.xml file in Managing Flash Communication Server Help.

To play MP3 files, attach a NetStream object to a Video object and call the play method, or attach it to the MovieClip object and call the attachAudio method. In the parameter that specifies streamname, or what to play, you must precede the MP3 file name with mp3:. The following code sample shows two ways to play the file bolero.mp3 in the network stream mystream:

//plays bolero.mp3 in the opened stream mystream, using a Video object
vidObj.attachVideo(mystream);
mystream.play("mp3:bolero");

//plays bolero.mp3 in the opened stream mystream2, using a MovieClip object
//bolero.mp3 is in the directory C:\mp3_files on the Flash Communication Server //computer, which is mapped to the virtual directory mp3dir, in Vhost.xml
movieObj.attachAudio(mystream2);
mystream2.play("mp3:mp3dir/bolero");

Tip: Although in a NetStream.play statement you can choose not to specify the default video/audio file format, FLV, you must always specify MP3 files. That is, "flv:granada" and "granada" will both play the file granada.flv. Only "mp3:bolero" will play the file bolero.mp3.

To play back the ID3 tags of MP3 files, precede the stream name with id3: and define a callback function to capture the ID3 data. For example, to display the ID3 tag of bolero.mp3:

//displays the ID3 tag of bolero.mp3
mystream.play("id3:bolero");

//callback function to capture the ID3 data. Data will be displayed with
//"info." preceding the data from the tag, for example, info.songtitle.
mystream.onId3 = function(info){
	for (i in info){
		trace(i + ":" + info[i]);
	}
}