How to load Subtitle text for Flash Video? - actionscript-3

A Video player is created using "Video" in flash action script 3.0. And played video using net stream. The sample code is:
connection = new NetConnection();
connection.connect(null);
on connection success stream and video crested and played.
stream = new NetStream(connection);
video = new Video();
video.width = stage.stageWidth;
video.height = stage.stageHeight;
video.attachNetStream(stream);
stream.play(videoURL);
Video is playing correctly. I want to display subtitle for the video. I have .srt formeted file for the video, any solution in as3 to load the SRT for the Video on flash.

Writing a .srt parser isn't that difficult. Use the CuePoint API provided by AS3 to add cuepoints to your Video instance at runtime. Then listen for the onCuePoint event and display the relevant text in a text field.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var client = {};
client.onCuePoint = function(info:Object):void
{
var key:String;
for (key in info)
{
trace(key + ": " + info[key]);
}
};
ns.client = client;
var vid:Video = new Video();
vid.attachNetStream(ns);
addChild(vid);
ns.play("video.flv");
Instead of tracing the output, you can display text in an on-screen text field.

Related

play a external flv video

Hello please someone can help me with this ...
I want to play a external flv video ("../sync/video/video.flv"), but in case the video is missing or when there is a (StreamNotFound) error
I want to play automatically another flv video.
case "NetStream.Play.StreamNotFound":
ns.play("../sync/filler/video2.flv");
but it doesn't work ....
here is the full code :
var vid:Video;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var customClient:Object = new Object();
customClient.onMetaData = metaDataHandler;
ns.client = customClient;
ns.play("../sync/video/video.flv");
vid = new Video();
vid.attachNetStream(ns);
addChild(vid);
function netStatusF(e:NetStatusEvent):void
{
switch (e.info.code)
{
case "NetStream.Play.StreamNotFound" :
ns.play("../sync/filler/video2.flv");
break;
}
}
function metaDataHandler(infoObject:Object):void
{
vid.width = infoObject.width;
vid.height = infoObject.height;
}
You have just to add a NetStatusEvent.NET_STATUS event listener to your NetStream object :
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusF);
Then you have to assure that your second video file exist, otherwise you'll have a looping problem.
Hope that can help.

AS3 browse and load video

I'm currently working on a AS3 AIR project which involves allowing the user to browse a video file and load that same video onto the stage. I've managed to allow the user to browse for a video file type and according to my traces it completes loading the video but this is as far as I've got. There is plenty of tutorials which involve how to load video files from local sources or external links but nothing to show me what to do with a browsed file to display it on the stage. Here is the code so far for browsing to the video file:
private function browseVideo():void {
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, videoFileSelected);
var videoTypeFilter:FileFilter = new FileFilter("Video files", "*.3g2; *.3gp; *.asf; *.asx; *.avi; *.flv; *.m4v; *.mov; *.mp4; *.mpg; *.rm; *.swf; *.vob; *.wmv;");
fileReference.browse([videoTypeFilter]);
}
private function videoFileSelected(e:Event):void {
fileReference.addEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.addEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
fileReference.load();
}
function onVideoFileLoaded(e:Event):void {
var fileReferenceTarget:FileReference = e.target as FileReference;
var data:ByteArray = fileReferenceTarget["data"];
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
var videoLoader:Loader = new Loader();
videoLoader.loadBytes(data);
videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onVideoLoaderComplete);
trace("video file loaded");
}
function onVideoFileLoadError(e:Event):void {
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
trace("video file load failed");
}
function onVideoLoaderComplete(e:Event):void {
var loadedContent:DisplayObject = e.target.content;
var loader:Loader = e.target.loader as Loader;
scope.addChild(loader);
}
To play a video using AS3 ( Flash ) you can use a Video object on which you can attach a NetStream object, you can also use an FLVPlayback component. For flex, take a look on my answer of this question where I put an example of playing a video stream. And in all cases, I think that you don't need a FileReference object because a File is suffisant to get the path of your local file and then play it with any manner you want.
Take a look on this example :
function browseVideo():void {
var file:File = new File();
file.addEventListener(Event.SELECT, videoFileSelected);
file.browse([videoTypeFilter]);
}
function videoFileSelected(e:Event):void {
playVideo(e.currentTarget.nativePath);
}
function playVideo(video_path:String){
// using a Video + NetStream object
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = this;
var video:Video = new Video();
video.attachNetStream(ns);
addChild(video);
ns.play(video_path);
// using an FLVPlayback component inserted on the Stage
flvplayback.load(video_path);
flvplayback.play();
}
For more details on how to work with video, you can take a look here, you can find all what you need to know about video ( loading videos, supported formats, cue points, ... ).
Hope that can help.

How to display mp4 then pause and replay in Flash CC

I’m resurrecting a simple banner where we used an FLV movie and the following code to have the movie pause using setInterval and then play again. It’s a simple butterfly movie that flaps it’s wings and then goes back to a static state.
Looks like FLVs are now discouraged in the more recent Flash versions so I would like to know how to make a movie play, then pause using setInterval, then replay.
Here is the working AS3 when using an FLV on the Timeline in a frame based animation but does not work with an imported .mp4:
stop();
function timesUp()
{
gotoAndPlay(1,"Scene 1");
clearInterval(itv);
}
var itv=setInterval(timesUp, 15000);
Do the new suggested methods use a single frame movie where you load and play the mp4, then use setInterval as a timer to replay?
I can’t find any tutorials that have this method as an example.
I did read a tutorial on the Adobe site but the following did not load and play the movie (morpho.mp4)
var nc:NetConnection = new NetConnection();
nc.connect(null);
var vid:Video = new Video();
addChild(vid);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void
{
// handle netStatus events, described later
}
function asyncErrorHandler(event:AsyncErrorEvent):void
{
// ignore error
}
vid.attachNetStream(ns);
ns.play("morpho.mp4");
Try this :
import flash.utils.Timer
import flash.events.TimerEvent
var video_playing:Boolean = false,
timer:Timer,
nc:NetConnection,
ns:NetStream,
video:Video,
video_name:String = 'video.mp4'
nc = new NetConnection()
nc.connect(null)
ns = new NetStream(nc)
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code){
case 'NetStream.Play.Start' :
if(timer.currentCount == 0){
timer.start()
}
break
}
}
function asyncErrorHandler(event:AsyncErrorEvent):void {
}
video = new Video(135, 135) // set the size of video to 135x135px
video.x = 165 // set the left of video to 300 - 135 = 165px
video.y = 0 // set the top of video to 0
video.attachNetStream(ns)
addChild(video)
timer = new Timer(1000, 3) // 3 seconds, just for example
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_on_Complete)
function timer_on_Complete(e:TimerEvent){
if(video_playing){
video_playing = false
ns.close()
} else {
start_video()
}
timer.reset()
timer.start()
}
start_video()
function start_video(){
ns.play(video_name)
video_playing = true
}

AS3 play multy video files

I have this code for playing flv video when swf starts. How can I make it play 2 or 3 flv videos in sequence? here is the code which loads flv and plays it so I need to play two more videos after the first one automatically.
var vid:Video = new Video(1080, 720);
addChild(vid);
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("Postvideo1.flv");
You are very close. NetStream client will help you to solve this task. More details on onPlayStatus
Establishes a listener to respond when a NetStream object has completely played a stream.
var listener:Object = {};
listener.onMetaData = function (meta:Object):void {
//Video duration
trace(meta.duration);
};
listener.onPlayStatus = function (data:Object):void {
if (data.code == "NetStream.Play.Complete") {
trace("Video playback is completed!");
//Good place to initiate playback of another video
}
}
ns.client = listener;

streaming video player / attaching video(not camera) to NetStream in actionscript 3

I am new to actionscript, basically i am trying to stream video player but we can't use attachVideo in as3 so what can i use instead of attachVideo in following code? Im using flash builder/flex 4.6 . If you could suggest link/tutorial for streaming video player it would be great help. Thank you!
private function initOutStream():void
{ /* streamOut is NetStream to adobe cirrus ,
conn is making connection to adobe cirrus */
trace("initOutStream");
streamOut = new NetStream(conn,NetStream.DIRECT_CONNECTIONS);
streamOut.addEventListener(NetStatusEvent.NET_STATUS,streamStatus);
streamOut.publish("media");
// mp4 file from local machine
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.play("t.mp4");
ns.client = this;
var vid:Video = new Video;
vid.attachNetStream(ns);
// streaming vid to media server
streamOut.attachVideo(vid);
var streamOutClient:Object = new Object();
streamOutClient.onPeerConnect = function(farStream:NetStream):Boolean
{
return true;
}
}
error:
1061: Call to a possibly undefined method attachVideo through a reference with static type
This is a code i wrote lately for a business job. i hope it helps...
var vid:Video = new Video();
vid.width = 640; vid.height = 480;
addChild(vid);
var cnn:NetConnection = new NetConnection();
cnn.connect(null);
var ns:NetStream = new NetStream(cnn);
ns.play("t.mp4");
vid.attachNetStream(ns);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, error);
ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
function streamStatus(e:NetStatusEvent){
//trace(e.info.code);
if(e.info.code == "NetStream.Play.Stop"){
}
}
function error(e){
}