ByteArray Access to NetStream in Flash Player 10.1 by Thibault Imbert

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) :

A Flash animation should have appeared here, but it seems that your browser has an older version of the Flash Player or it is not installed at all. Please, install the last release of the Flash Player now, then reload this page.

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)

  1. 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()

    Wednesday, May 12, 2010 at 10:08 am #
  2. 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

    Wednesday, May 12, 2010 at 10:09 am #
  3. Danny Kopping wrote::

    Dude, this is fucking amazing… My mind is buzzing with the possibilities this opens up!!
    Thanks :)

    Wednesday, May 12, 2010 at 10:16 am #
  4. why wrote::

    it really sucks that you cant use this with different headers than flv. It’s a very nice feature anyway.

    Wednesday, May 12, 2010 at 10:22 am #
  5. Drew wrote::

    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?

    Wednesday, May 12, 2010 at 11:51 am #
  6. 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!

    Wednesday, May 12, 2010 at 2:45 pm #
  7. Thibault Imbert wrote::

    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

    Wednesday, May 12, 2010 at 6:24 pm #
  8. Florian wrote::

    Thanks. I’ve posted an example with source code here:
    http://www.video-flash.de/index/netstream-bytearray-abspielen/
    (German)

    Thursday, May 13, 2010 at 12:03 am #
  9. Thibault Imbert wrote::

    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

    Thursday, May 13, 2010 at 12:24 am #
  10. jeanphilippe wrote::

    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

    Thursday, May 13, 2010 at 7:19 am #
  11. Thibault Imbert wrote::

    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

    Thursday, May 13, 2010 at 8:13 am #
  12. Benny wrote::

    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.

    Saturday, May 15, 2010 at 6:36 pm #
  13. Ole jak wrote::

    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?

    Sunday, May 16, 2010 at 1:37 pm #
  14. charlierun wrote::

    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???

    Monday, May 24, 2010 at 4:05 pm #
  15. Bhanuprakash wrote::

    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.

    Tuesday, May 25, 2010 at 8:14 am #
  16. Bhanuprakash wrote::

    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.

    Wednesday, May 26, 2010 at 9:13 am #
  17. Laurent wrote::

    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.

    Friday, June 4, 2010 at 1:53 pm #
  18. JesusYamato wrote::

    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 :)

    Sunday, August 15, 2010 at 1:36 am #

Trackbacks/Pingbacks (3)

  1. [...] ByteArray Access to NetStream in Flash Player 10.1 [ by Thibault Imbert ] [...]

     
  2. Summer AIR Mobile Contest 2010 CZ/SK — FlashRealtime.com on Wednesday, July 7, 2010 at 6:55 pm

    [...] 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 [...]

     
  3. [...] 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. [...]