There are moments like this, when you read the documentation of the Flash Player and you discover and nice new API that you have been waiting for such a long time. Do you see what I mean ? Yes, you read it right, ByteArray access to NetStream class is coming and it works beautifully in Flash Player 10.1. As many of you, I was also waiting for this API when working on my FLV editing library and this day has come.
The API is called appendBytes on the NetStream class used in conjunction with the NetStreamAppendBytesAction class. It allows you to inject a ByteArray to the NetStream object and play the video. This API was designed originally for HTTP streaming, allowing forward seeking into HTTP delivered-video, but if you just load a ByteArray locally through the FileReference API, voilà, you got local preview of video files without server round trip. If you read the documentation you'll notice that the API only works for now with FLV header-based files, but all codecs are supported (Sorenson, H.264, On2 VP6 and even AAC audio).
Here is a little demo which illustrates the API in the Flash Player 10.1 (make sure you are using the Flash Player 10.1 RC4 available on labs.adobe.com) :
The API is really simple :
// retrieve the FLV stream var bytes:ByteArray = event.currentTarget.data; // put the NetStream class into Data Generation mode netstream.play(null); // before appending new bytes, reset the position to the beginning netstream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); // append the FLV video bytes netstream.appendBytes(bytes);
This will allow new kind of apps and remove one more limitation that we had in the past
Comments (18)
Very exciting! However, I’m getting an error when trying out the example:
ReferenceError: Error #1069: Property appendBytesAction not found on flash.net.NetStream and there is no default value.
at TestAppend/onComplete()
Hey Thibault, some days ago I tried that new api. I wanted to use the bytearray access for broadcasting audio files(like aac or mp3) in Air to a RED5 server and than the red5 server would send it to its clients(also in air). But I failed to do this with the appendbytes. Do you know if this is possible with the appendbytes method? Big thanks, Maxim Gladines
Dude, this is fucking amazing… My mind is buzzing with the possibilities this opens up!!
Thanks
it really sucks that you cant use this with different headers than flv. It’s a very nice feature anyway.
Thanks for posting Thibault!
I have a situation where I need to read from huge files starting at varying points and until user hits stop, however the files aren’t streamed in the normal way so everything tries to download.
I wanted to use a bytearray to control the download and feed an Sound object in streaming mode, building up a buffer of a few seconds (since feeding blocks via SampleDataEvent.SAMPLE_DATA seems to often give crackly results). Any idea whether bytearrays can now be fed to Sound?
This is very exiting, in fact we have been discussing this matter the last week and I was about to write my own NetStream class to make it work. This is the best news for some time (alot of good news has been pumping out from adobe lately so…
)
@Simon, you must run 10.1 rc4 to make it work. I made a similar demo to the one Thibault posted and it works great!
Hi Maxim,
Not at the moment, but this is something we are considering maybe for a future enhancement of this feature.
Hi Drew,
Unfortunately a Sound.loadBytes() API is not there yet. This one is also on my list and we know it would be a really valuable API. Please spread the word about this feature request and keep voting :
https://bugs.adobe.com/jira/browse/FP-1651
It is the best way to make it happen
best,
Thibault
Thanks. I’ve posted an example with source code here:
http://www.video-flash.de/index/netstream-bytearray-abspielen/
(German)
Hi Florian,
Sweet! It’s better with code you are right.
Actually I just added a little code sample. You do not have to recreate the NetConnection object you can reset the pointer by calling the appendBytesAction APi and reset the parser.
best,
Thibault
Hi Thibault,
What do you say by “You do not have to recreate the NetConnection object you can reset the pointer by calling the appendBytesAction APi and reset the parser” ?
Can you show us a complete code
Thanks a lot
JP
Hi jeanphilippe,
Florian’s code is complete. It’s just that it is not necessary to recreate the NetConnection and NetStream objects over and over. Just create those once, and reset the NetStream object by using the NetStreamAppendBytesAction API.
best,
Thibault
Hi Thibault,
first of all, thanks! This was exactly what i’ve been looking for!
I do have an non-documented issue though.
I am splitting the bitearray before reassembling and pushing it to the netstream, however, the appendbytes() (in a loop) breaks there.
The ‘packs’ we send seem to only result in playback when the packs are at least half the size of the original file.
So when we take a 1MB video file, split it into 2 byte arrays that we append in a foreach loop, it works. As soon as the byte arrays are less than half the video file, it does nothing…
any idea if is this is a built in restriction, a bug or some faulty logic on our side?
Thanks,
Benny.
So now we (for what ever reson we have) finaly can get speex encoded data from microphone when using P2P connection to for example save speex encoded audio file?
Hi guys great news with this flash10.1 beta player Im trying to get my hands dirty on it. Im experiencing trouble calling netStream seek my video freezes,Im calling on Seek.Notify _ns.appendBytesAction(“resetSeek”) but its not working any ideas???
Hi Thibault Imbert,
The new API appendBytes method on NetStream Object working fine for strictly FLV encoded files, When I use H.264 encoded ByteArray stream with FLV Header base file using appendBytes method on NetStream with Play(null), It’s neither giving any error message nor dispalying any video frame.
Please give me suggession to solve my problem.
and I already tracking a bug on Adobe.
http://bugs.adobe.com/jira/browse/FP-5
Thanks,
Bhanuprakash.
Hi Thibault Imbert,
Is there any API( in ActionScript code ) embedding H.264 stream into FLV header base file (.flv)?. Because AppendByte mehtod of NetStream class works only for FLV header files.
Thanks,
Bhanuprakash.
Hey Thibault,
Is it possible to publish a netstream, to a rtmfp server that is created with this technique? Don’t seem to get that working.
Thanks to this post, i knew right away what i had to do : Make a MKV loader for Flash and now it’s done :
http://coalguys.com/jesusCorner/MKVP.html
Source is here : http://code.google.com/p/mkvloader/
The source is way more advanced than the demo page, i haven’t updated it with the lastest svn yet
Trackbacks/Pingbacks (3)
[...] ByteArray Access to NetStream in Flash Player 10.1 [ by Thibault Imbert ] [...]
[...] Twitter, GPS, maps, with latest AIR 2.0 features such as multi-touch, accelerometer, audio/video NetStream appendBytes access and so on. Important: The app must be built originally for this contest, but we surely [...]
[...] Viewing the video Flash Player 10.1 added new NetStream function called appendBytes, which enables you to pass ByteArray containing FLV bytes to a NetStream and then display it. This was originally added for HTTP streaming, but we can leverage it nicely as well. You can read more about it here. [...]