Working with MP3 files

In Flash Media Server applications, you can use client-side ActionScript to play MP3 audio files and to display the ID3 tags of MP3 files, and use server-side 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. (Flash Media Server 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 Media 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 Registering client applications in Managing Flash Media Server.

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 filename 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_ns, using a Video object
vidObj_video.attachVideo(mystream);
mystream_ns.play("mp3:bolero");

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

TIP

 

In a NetStream.play() statement you can choose not to use the default video/audio file format, FLV. However, in order to play MP3 files, you must always specify the MP3 format. That is, "flv:granada" and "granada" will both play the file granada.flv but only "mp3:bolero" will play the file bolero.mp3.

To display 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_ns.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_ns.onId3 = function(info){
    for (i in info){
        trace(i + ":" + info[i]);
    }
}

About supported versions of ID3 tags

Flash Media Server supports playback of ID3 text tags in UTF-8, UTF-16, and ISO-8859-1 formats and supports ID3 versions 1.0, 2.3, and 2.4. Only tags that contain text data are supported, such as song title, artist name, comments, and year recorded.

Controlling MP3 files with server-side ActionScript

You can use methods of the server-side Stream object to play back an MP3 file (Stream.play()) and get the length of an MP3 file (Stream.length()). For more information on using these methods with MP3 files, see the Stream.play() and Stream.length() entries in the Server-Side ActionScript Language Reference.

To delete MP3 files using server-side ActionScript, you can use the Application.clearStreams() method.

To publish MP3 files, over a stream, or the ID3 tag information associated with MP3 files, use the Stream.play() method, as shown in the following example:

// Set up the server stream
application.myStream = Stream.get("music");
if (application.myStream)
{
// Publish the MP3 file bolero.mp3 to the stream "music"
// Use the mp3: prefix in front of the stream name and specify 0 for the startTime parameter to indicate the server should play the recorded stream bolero.mp3
    application.myStream.play("mp3:bolero", 0, -1);
}

To use the Stream.play() method to capture and display the text of ID3 tags, see the following example:

// Set up the server stream
application.myStream = Stream.get("description");
application.myStream.onId3 = function(info)
{
    for (i in info)
    {
        trace(i + ": " + info[i]);
    }
}
if (application.myStream)
{
    // Publish the ID3 text tag data of bolero.mp3 to a stream "description".
    // Use the id3: prefix in front of the stream name and specify 0 for the startTime parameter
    application.myStream.play("id3:bolero", 0, -1);
}