Search code examples
flashactionscript-3netstream

appendBytes() not working on actionscript 3


I have the following code

var playingStream:NetStream;
function playBytes(bytes:ByteArray): void
{

            var connect_nc:NetConnection = new NetConnection();
        connect_nc.connect(null);
            playingStream=new NetStream(connect_nc);
            playingStream.soundTransform= mySound;
             playingStream.play(null);
            playingStream.appendBytes(bytes);
}

I am calling this function from JS using the external interface callback But as soon as I compile the above code in the .fla file, it throws an error.

Error:1061 Call to a possibly undefined method appendBytes through a reference with static type flash.net:NetStream

The flash help page shows that the function is valid in AS3 . Where am I going wring then ??


Solution

  • The code you have should work however, the appendBytes method was included in Flash Player 10.1 and newer. If you are compiling against anything lower then 10.1, you would get an error similar to the one listed in your question. Check out the following blog post at ByteArray.org, and the example I've included below. I built the example for a different post and I hope it helps you out as well!

    AppendBytes


    Playback Initialization:

    var video:Video = new Video(width, height);
    var video_nc:NetConnection = new NetConnection();
    var video_ns:NetStream = new NetStream();
    
    video_nc.connect(null);
    video_ns.play(null);
    video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
    
    video.attachNetStream(video_ns);
    

    ProgressEvent.PROGRESS Handler:

    video_ns.appendBytes(bytesAvailable);
    

    This is essentially the jist of it, bytesAvailable will represent the read bytes from the event data buffer. A full example is listed below:

    package
    {
    
    import flash.display.Sprite;
    import flash.events.NetStatusEvent;
    import flash.events.ProgressEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.net.NetStreamAppendBytesAction;
    import flash.net.URLRequest;
    import flash.net.URLStream;
    import flash.utils.ByteArray;
    
    [SWF(width="1280", height="720")]
    public class NetStreamAppendBytes extends Sprite
    {
    
        var video:Video;
        var video_nc:NetConnection;
        var video_ns:NetStream;
    
        var video_stream:URLStream; 
    
        public function NetStreamAppendBytes()
        {
            super();
    
            video_nc = new NetConnection();
            video_nc.connect(null);
    
            video_ns = new NetStream(video_nc);
            video_ns.client = this;
            video_ns.addEventListener(NetStatusEvent.NET_STATUS, ns_statusHandler);
    
            video = new Video(1280, 720);
            video.attachNetStream(video_ns);
            video.smoothing = true;
    
            video_ns.play(null);
            video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
    
            video_stream = new URLStream();
            video_stream.addEventListener(ProgressEvent.PROGRESS, videoStream_progressHandler);
    
            video_stream.load(new URLRequest("path_to_flv"));
    
            addChild(video);
        }
    
        private function ns_statusHandler(event:NetStatusEvent):void
        {
            trace(event.info.code);
        }
    
        private function videoStream_progressHandler(event:ProgressEvent):void
        {
            var bytes:ByteArray = new ByteArray();
    
            video_stream.readBytes(bytes);
            video_ns.appendBytes(bytes);
        }
    
    }
    
    }
    

    Best of luck!