Search code examples
flashaudioflash-cs4red5recording

How to capture audio from browser and save it on a server?


I'm currently working on a project - an online education system, and I need to make it possible for studs to introduce themselves in a 30 sec audioclip.

I need to implement it with Adobe Flash. The problem is that I have no idea how the Flash + Red5 duo work together. There aren't that many helpful recourses online, at least for me since I'm a beginner at Flash. (I do mostly PHP stuff.)

1) When you connect to the server, how do you make it record audio from flash client?

2) After 30 secs, how do you stop recording and save file in a specific folder on a server?

3) How do I move this file to the server's HTTP folder, so that I could access it from homepage after that?

Please note I'm a beginner at flash and Red5, so I really need detailed explonation from you guys.

Thanks a lot!


Solution

  • I'll try my best to make the answer clearly.

    1) When you connect to the server, how do you make it record audio from flash client?

    First of all, you need to know the connection between server and client used protocol like RTMP. So in the server side, we need to setup our address like rtmp://127.0.0.1/demoServer(in red5 demoServer is your app name). Next in the Flash side, we can connect server by NetConnection:

        import flash.net.NetConnection;
        public var nc:NetConnection;
        nc = new NetConnection();
        nc.connect("rtmp://127.0.0.1/demoServer");
    

    I could definitely tell you, the 80% work are in the Flash-Client side. In order to capture voice, we need setup our Microphone:

        import flash.media.Microphone;
        public var mic:Microphone;
        mic = Microphone.getMicrophone();
    

    After that we need a pipe to transport voice captured form microphone. Fortunately we have NetStream:

        import flash.net.NetStream;
        private var stream:NetStream;
        var sm:NetStream=new NetStream(nc);
        stream.attachAudio(mic);
    

    The connection just like building a bridge so that stream can carry stuff form client to server. OK, the last thing wo need to do is publish:

        stream.publish("some name","record");
    

    Now, you can see a .flv file named some name in the server side. This file will get bigger if you have opened microphone.

    2) After 30 secs, how do you stop recording and save file in a specific folder on a server?

    Create a 30 secs timer that begins at recording. Closing the stream When time out:

        import flash.utils.Timer;
        t = new Timer(1000, 30);
        t.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
        private function timerComplete(event:TimerEvent):void{
          //close the stream
          stream.close();
          mic.setSilenceLevel(0);
        }
    

    By default, red5 will save file in \webapps\dictRed5Server\streams. If you want to change this, check this guide.

    3) How do I move this file to the server's HTTP folder, so that I could access it from homepage after that?

    Red5 can work together with apache tomcat and you can use a flv player to play those records.

    I hope above snippets can help you. I suggest to you that can install red5 and run some demos and google what you do not understand.