AS2: Record & Save Audio To Server With Flash Media Server

Download Example Files

The other day I posted a tip on how to set up Flash Media Server Developer Edition locally on your PC. To reiterate, I'm by no means an expert with Flash Media Server and this was the only test I had done with it, but I figured I'd show how to record some audio using your microphone and FMS and save it on to the FMS server as an .flv file. Let's take a look at the code:

  1. stop();
  3. var count:Number          = 0;
  4. var timestamp:Date    = new Date();
  5. var nc:NetConnection       = new NetConnection();
  7. nc.connect("rtmp://localhost/audiotest/samples");
  9. var ns:NetStream          = new NetStream(nc);
  10. ns.setBufferTime(2);
  12. var mic:Microphone    = Microphone.get();
  13. mic.setRate(22);
  15. ns.attachAudio(mic);
  17. ns.onStatus = function($info:Object):Void
  18. {
  19.     trace($info.code);
  20. };
  22. // record the audio to the stream
  23. function recordAudio():Void
  24. {
  25.     var fileName:String = String(timestamp.getTime() + (count++));
  27.     ns.publish(fileName, "record");
  28. }
  30. // stop the recording of audio to the stream
  31. function stopRecordingAudio():Void
  32. {
  33.     ns.publish(false);
  34. }
  36. // plays back the audio that was recorded
  37. function playRecordedAudio():Void
  38. {
  40. }
  42. // record button
  43. recordBtn_mc.onRelease = function():Void
  44. {
  45.     recordAudio();
  46. };
  48. // stop button
  49. stopBtn_mc.onRelease = function():Void
  50. {
  51.     stopRecordingAudio();
  52. };
  54. // play button
  55. playBtn_mc.onRelease = function():Void
  56. {
  57.     playRecordedAudio();
  58. };

As you can see, I'm still making a connection to my localhost on my PC. This example assumes that you have an "audiotest" folder in your "applications" folder in FMS (refer to the tip here to see how to set up your file structure). Inside of that folder you have your "streams" folder and in there you have a folder called "samples" which is the destination directory that the .flv files will be saved to when you stop recording.

After that we have our standard code for connecting to the stream and getting the microphone. The next thing I want to go over is the recordAudio() method. I'm creating a random filename using the Date object to try to make the filename as random as possible. I was also adding a variable that I was increasing by one each time to the end of the filename as for some reason when I tried this out originally the filename that was being created was the same every time. By adding count to it, I ensure that during that users session the filenames created won't be the same. The idea here is that you'll have multiple users on the site at the same time using the service so creating the files using the date makes the chance of it being named the same very small. For the current users session, you'll increase by count to ensure that that user's filenames don't mix up as well. Ideally, you'd want to create a safer naming convention to prevent the possibility of creating the same filename for multiple users, but for this example this will suffice.

The last thing we want to note about the filename is that it does not end in the extension .flv. Just like I stated in the tip, you don't need to add the extension to the end of the filename as FMS automatically saves it out as an .flv file.

If you want to convert your .flv to an audio format for later playback, you're opening up a whole pandora's box of issues and you'll be on your own in trying to figure that out. Flash Media Server uses the NellyMoser codec to encode the audio and it's close to impossible to extract the audio unless you a) want to pay about $7,500 to NellyMoser to extract it or b) use a third party command line tool which, from my research, is a pretty hit or miss process.

Saving the file as an .flv on the server (or your hard drive in the "samples" folder if you're following this example) should suffice enough in that you can later play back the .flv file in Flash. With a combination of slick Flash programming and some server-side scripting you can make a nice little application that captures the audio, saves it to the server, saves the filename in the database, and you can later retrieve it for playback.

Here is a blog post that may help you out with some of this audio recording and converting to a different file format: Andrew Paul Simmons' Post

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.


I could play the swf only when it is saved on the desktop. when i save the file in any of the local disks, the swf doesn't show the flash settings pop up. please help.

Leave a comment