« Back To Articles List

Is it possible to insert or overwrite into the middle of an audio stream?

Graeme Bull - Jun 20, 2006

This one is a very good question and actually comes up quite often. I've seen it on forums and mailing lists. It gets answered but I guess people don't want to search. So it makes a good quick tutorial here on this site with all the rest of them :)

The question: Is it possible to insert or overwrite into the middle of an audio stream?

The answer:

Yes. You have to do this server side though.

The theory:
What you need to do is make a copy of the FLV up to the spot you want to insert your new piece in. Then you play the new piece and append it to the original. Then you take the remainder of the FLV and append it to the new FLV you are creating. This is really hokey and actually requires you take the complete time that both FLV files have. If you have a 5 minute FLV and you want to add in a short 20 second clip to the middle of it, this process will take 5 mins and 20 seconds.

So you take stream A, the original stream you want to add something into (all of this is server side)

oldStream = Stream.get("A");

Grab the piece you want to add in
smallClip = Stream.get("B");

Make a new stream to record into
newStream = Stream.get("newFile");

Make the new stream record the old file
newStream.play("oldFile");
newStream.record("record");

Then you need to monitor it with an interval of something short like every 100 milliseconds. When it reaches the time of whatever you want the new clip to go into then stop playing the old stream and put in the new one:

newStream.play(false);

newStream.play("smallClip");

** side note **

You can also at this point tell the server to stop recording too, and then use the append value in the record method to add onto the current FLV.

newStream.record(false)
newStream.play("smallClip");
newStream.record("append");

** end side note **

Watch the onStatus and see when it ends, you should get a "NetStream.Play.Stop" event, when you get that, then just play the other clip again, starting from the point where you finished off:

newStream.play("oldFile", 25); //25 is the seconds time you want the clip to start from.

When it finishes playing again then just stop recording and you should have a stream that has another stream in it. Delete the old one if you want I suppose.

So you can see it's a crappy tedious process, but yes, it can be done :)