I am trying to play and control a video, and one of key elements I need to control it is it's duration. I wrote a function that plays, pauses, resumes and stops videos, but, if I use slider to seek video near the end, I get an error saying that time is wrong.
I tried to trace time and to subtract 0.5 and 1 second. In that case, video is 2-5 seconds longer than expected?! Wierd.
myVideoData = new Video();
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.client = {};
ns.client.onMetaData = ns_onMetaData;
ns.client.onCuePoint = ns_onCuePoint;
myVideoData.attachNetStream(ns);
ns.play(menu.videolist.selectedItem.data); //Video is loading and playing just fine
function ns_onMetaData(item: Object): void {
myVideoDataW = item.width;
myVideoDataH = item.height;
myVideoDuration = item.duration;
//Below this line is added for testing.
ns.seek(item.duration); //It fails as Error #2044: Unhandled NetStatusEvent:. level=error, code=NetStream.Seek.InvalidTime
//Tried to ns.seek(item.duration-1); and it works, except it seeks video not 1, but 2-5 seconds, depending on video length
//I have same issue for every video, and I have tried like... 50-ish...
}
I just want to know is it about the code, about me...? Is it possible that all 50 videos I have tried have same problem? Sources of my videos are from my phone, from youtube, from professional web stores. All files are mp4!
(1)
Make sure
myVideoDurationis known or set to= 0;as starting point. For example: Since yourns_onMetaDatafunction updates the duration, there you could also enable seeking with a:Then you could use this logic to seek:
(2)
That's because
.durationis a Number data-type but.seekactually expects an int value.The difference is...
Number = 16.005;vsint = 16;. Number includes a decimal point. NetStream's.seekexpects a whole numerals without fractions (basically: Just use integers, no decimal points).Solution:
Simply cast the
.durationinto an int data type.Here's a testable example based on your code. Give it a file called
video.mp4at same location as compiled.