Showing videos in OpenFl

No problem. So basically, you have to create a NetConnection instance somewhere and add an event listener.

private var nc:NetConnection = new NetConnection();

// [....]

nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStreamConnect);
nc.connect(null);

and then in the event listener, you create a NetStream instance, append bytes from your video asset and attach the stream to an instance of the video class:

 public function onNetStreamConnect(evt:NetStatusEvent):Void

        if (evt.info.code == 'NetConnection.Connect.Success') {

            var ns:NetStream = new NetStream(nc);

            try {
                
                var bytes = Assets.getBytes("path/to/your/video/in/your/assets");
                ns.play(null);
                
                // using appendByte, you can play a video embeded as an asset
                ns.appendBytes(bytes);
                 
                var vid:Video = new Video();
                vid.attachNetStream(ns);
                
                // Now, you just have to add the video to the display list to make appear on the screen
                addChild(vid);

            } catch(error:Dynamic) {

                trace(error.message);

            }
        }
    }

You can find many other example in action script 3 to work with these classes.

NB: you can also play a video without embedding it your swf. To do that, forget about the “appendByte” part et directly play the URL:

ns.play("http://www.to-your-video.com/video.mp4");
var vid:Video = new Video();
vid.attachNetStream(ns);
                    
// Now, you just have to add the video to the display list to make appear on the screen
addChild(vid);
1 Like