Archive for the tag '.flv'

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:

Actionscript:
  1. stop();
  2.  
  3. var count:Number          = 0;
  4. var timestamp:Date    = new Date();
  5. var nc:NetConnection       = new NetConnection();
  6.  
  7. nc.connect("rtmp://localhost/audiotest/samples");
  8.  
  9. var ns:NetStream          = new NetStream(nc);
  10. ns.setBufferTime(2);
  11.  
  12. var mic:Microphone    = Microphone.get();
  13. mic.setRate(22);
  14.  
  15. ns.attachAudio(mic);
  16.  
  17. ns.onStatus = function($info:Object):Void
  18. {
  19.     trace($info.code);
  20. };
  21.  
  22. // record the audio to the stream
  23. function recordAudio():Void
  24. {
  25.     var fileName:String = String(timestamp.getTime() + (count++));
  26.    
  27.     ns.publish(fileName, "record");
  28. }
  29.  
  30. // stop the recording of audio to the stream
  31. function stopRecordingAudio():Void
  32. {
  33.     ns.publish(false);
  34. }
  35.  
  36. // plays back the audio that was recorded
  37. function playRecordedAudio():Void
  38. {
  39.     ns.play(currentFileName);
  40. }
  41.  
  42. // record button
  43. recordBtn_mc.onRelease = function():Void
  44. {
  45.     recordAudio();
  46. };
  47.  
  48. // stop button
  49. stopBtn_mc.onRelease = function():Void
  50. {
  51.     stopRecordingAudio();
  52. };
  53.  
  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

Tags: , , , , ,

Setting Up Flash Media Server Developer Edition For Newbs

A couple of weeks ago I had the honor of doing a little Flash Media Server work for the first time in my life. I had never worked with FMS nor did I know a lick about it, but I Googled it to a frenzy and found some useful little things. Without having access to Flash Media Server, which will cost you an arm and a leg, you can download the free developer edition to test your stuff locally. I normally work on a Mac but I had to do this on my PC laptop as I'm not very good with server-side stuff and I don't know how to run FMS developer edition on the Mac. Keep in mind, this is for newbies only and probably not the way that an FMS pro would set up their work, but this served me well in my trials and tribulations.

Anyway, you can grab the developer edition here. After you've got it downloaded, go ahead and go through the installation. The default installation directory is located at C:\Program Files\Macromedia\Flash Media Server 2. In there you will find an applications folder which is where all your projects will sit (and by projects I mean your .FLV file streams as you can put your working FLA anywhere on your computer).

The first thing you should do is create a folder inside of the applications folder that will hold your videos. I cleverly titled mine "videos". Inside of the videos folder, lets create a "streams" folder. This streams folder is the key as this is where your files will sit. Without this folder, nothing will function properly. GUARD IT WITH YOUR LIFE! Inside of the streams folder, I then created a folder for my actual streams to go into. I named mine "fmstest". Finally, in the fmstest folder, I have my FLV file, let's call it "dance.flv".

Now, to play this file, you'll need to establish a connection to the server. You'd do so like this:

Actionscript:
  1. var nc:NetConnection = new NetConnection();
  2. var ns:NetStream = new NetStream(nc);
  3.  
  4. vid.attachVideo(ns);
  5.  
  6. nc.onStatus = function($info:Object):Void
  7. {
  8.     if ($info.code == "NetConnection.Connect.Success")
  9.     {
  10.         playStream();
  11.     }
  12. };
  13.  
  14. function playStream():Void
  15. {
  16.     ns.play("dance");
  17. }
  18.  
  19. nc.connect("rtmp://localhost/videos/fmstest");

We have a couple of things to note here. The first thing to make sure of is that you have a video object on the stage with an instance name of "vid". This is the object we'll use to load our movie into.

The second is to note the path that you're connecting to in the NetConnection. I'm using localhost because we are running this locally, but you would replace this with whatever the IP or address would be to your server. Also in the path, notice that I do not define my applications folder. FMS will know that your files are in the applications folder by default. Lastly, the path avoids the streams folder. I've still yet to figure out why you don't need to define the streams folder in the connection path, but that's how it works and I wasn't about to question it. :)

Also note that when you write the filename of the file you are going to play, you do not add the .flv extension. Again, something that works and I have no explanation for.

This is the barebones that you need to run the video locally through FMS. I also found this Flash Media Server Eclipse plugin but I found it after I had finished up my FMS experiments and have yet to use it so use at your own risk.

I really wish I could offer more help on FMS but unfortunately my knowledge is limited to only the tests I have run. If you'd like some more information, two resources to research that were vital in my own experiments would be the "Ask an FMS Guru" series and FlashComGuru. Good luck!

Tags: , , , , ,