View Full Version : Playing the next .flv in a XML driven list component.
FaceEraser
08-24-2005, 03:13 PM
Hi all...
Baically, I'm fairly new to actionscript, and definately new to using Flash Video. I followed Lee Brimelow's (excellent) video tutorials at www.gotoandlearn.com and discovered a lot about both. Here's my problem.
I have a flash video driven by a XML playlist set up in a list component as per Lee's tutorial. When the video currently playing comes to an end, I need the next listed video to automatically begin.
I looked around these forums and found a bit of code that was a little help, but had no luck in solving all my issues.
Here's some code:
// Here is where the list retrieves the XML list data
var vlist:XML = new XML();
vlist.load("xml/videos.xml");
vlist.ignoreWhite = true;
vlist.onLoad = function() {
var videos:Array = this.firstChild.childNodes;
for(i=0;i<videos.length;i++) {
listWin_mc.videoList.addItem(videos[i].attributes.desc, videos[i].attributes.url, videos[i].attributes.cue);
}
ns.play(listWin_mc.videoList.getItemAt(0).data);
listWin_mc.videoList.selectedIndex = 0;
}
var vidList:Object = new Object();
vidList.change = function() {
ns.play(listWin_mc.videoList.getItemAt(listWin_mc. videoList.selectedIndex).data);
}
listWin_mc.videoList.addEventListener("change", vidList);
// Here is some code to determine if the video has ended.
// I can't figure out how to go to the next video and highlight the next list item
// when the video ends
onEnterFrame = function () {
getTotalTime();
totalPlayingTime = Math.round(duration);
currentPlayingTime = Math.round(ns.time);
if (totalPlayingTime == currentPlayingTime) {
// I'm guessing this is where I'd put the code to trigger the next list item
}
}
Any help on this would be much appriciated and get me out of this rut!!!
- DB
cancerinform
08-24-2005, 03:46 PM
Try this, should work
i=1;
onEnterFrame = function () {
getTotalTime();
totalPlayingTime = Math.round(duration);
currentPlayingTime = Math.round(ns.time);
if (totalPlayingTime == currentPlayingTime) {
listWin_mc.videoList.selectedIndex = i;
listWin_mc.videoList.addEventListener("change", vidList);
ns.play(listWin_mc.videoList.getItemAt(i).data);
i++;
if (i>=videos.length) {
i = 0;
}
}
}
FaceEraser
08-24-2005, 04:20 PM
Thanks cancerinform, but the video just goes to the beginning of the first video and plays, rather that moving to the next one.
I think see what you're trying to do and I keep playing withit... but to no avail.... Drat....
cancerinform
08-24-2005, 04:28 PM
Ok,
check this example which I made to try out your code:
var vlist:Array = new Array();
for (i=0; i<5; i++) {
label1 = myList.getItemAt(0).data;
myList.selectedIndex = 0;
}
var vidList:Object = new Object();
vidList.change = function() {
myList.getItemAt(myList.selectedIndex).data;
};
myList.addEventListener("change", vidList);
s = 0;
i = 0;
onEnterFrame = function () {
s++;
if (s>=20) {
s = 0;
trace(i);
myList.selectedIndex = i;
myList.addEventListener("change", vidList);
i++;
if (i>=5) {
i = 0;
}
}
};
and this works. If it does not work it may have something to do with the video timing. Check that by tracing. I use a different way of timimg:
http://www.flashscript.biz/MX2004/flv_player/flv_player.html
cancerinform
08-24-2005, 05:21 PM
I thought about it again and one problem may also be that the onEnterFrame event is continuing. Have you tested that?
FaceEraser
08-24-2005, 05:47 PM
Yeah, it looks like the onEnterFrame keeps getting called up and the selected items just keep getting selected one after another.
cancerinform
08-24-2005, 06:05 PM
You can try this and set the time back to 0. Also the if statement should be the other way round.
i=1;
onEnterFrame = function () {
getTotalTime();
totalPlayingTime = Math.round(duration);
currentPlayingTime = Math.round(ns.time);
if (currentPlayingTime >= totalPlayingTime) {
currentPlayingTime=0;
listWin_mc.videoList.selectedIndex = i;
listWin_mc.videoList.addEventListener("change", vidList);
ns.play(listWin_mc.videoList.getItemAt(i).data);
i++;
if (i>=videos.length) {
i = 0;
}
}
}
FaceEraser
08-24-2005, 06:24 PM
This script you have here works, but only when switching from the first to second video. When the second video ends, I need it to go to the third (and the third to the fourth and so on). Right now, when the second video ends, it jumps to the first and plays.
Sorry I'm being such a pain in the ass. I've been learning rather quickly up to this point. Thanks so much for your help thus far.
Here's the code that was working as described above:
i=1;
onEnterFrame = function () {
getTotalTime();
totalPlayingTime = Math.round(duration);
currentPlayingTime = Math.round(ns.time);
if (currentPlayingTime == totalPlayingTime) {
listWin_mc.videoList.selectedIndex = i;
listWin_mc.videoList.addEventListener("change", vidList);
ns.play(listWin_mc.videoList.getItemAt(i).data);
i++;
if (i>=videos.length) {
i = 0;
}
}
}
If it helps at all, here's a link to what I have thus far. PLeas note that this in only the temporary interface and not the final product.
I've set the video to cue different slides in a movie clip. Now i need the video to jump to the next when it's complete (and eventually call up a new cue sheet, but that's the next monster).
Here's the link:
http://www.dmeducate.com/pitt/
cancerinform
08-24-2005, 06:30 PM
The reason why the script works for only one movie is because i continues to go up and is set back to 0. That is why I showed you the alternative to stop the onEnterFrame event within the if statement. have you tried that?
FaceEraser
08-24-2005, 06:38 PM
Yeah, I tried the
if (currentPlayingTime >= totalPlayingTime) {
but the movie never plays and actually slows my computer down for some bizzare reason.
cancerinform
08-24-2005, 06:51 PM
I think the main problem is that the timing is not set to the new movie, since there is no reference to the duration of the new movie. You can try this.
[as]
i=1;
function startMovie(){
this.onEnterFrame = function () {
getTotalTime();
totalPlayingTime = Math.round(duration);
currentPlayingTime = Math.round(ns.time);
if (currentPlayingTime == totalPlayingTime) {
delete this.onEnterFrame;
listWin_mc.videoList.selectedIndex = i;
listWin_mc.videoList.addEventListener("change", vidList);
ns.play(listWin_mc.videoList.getItemAt(i).data);
i++;
if (i>=videos.length) {
i = 0;
}
startMovie();
}
}
}
Or as I mentioned, check my tutorial and measure the time for every movie and then set it manually. That is actually what Macromedia recommends for their mediaplayer.
FaceEraser
08-24-2005, 07:36 PM
Perhaps "i" needs to equal the current selection's number in the array created by the XML.
Right now, it's always set as 1, therefore everytime "i" is called, or added(++) to, wouldn't it always be adding to 1, rather than the current selection's number in the array?
Is my line of thinking way off?
Damn me and my designer's brain.
cancerinform
08-24-2005, 09:23 PM
Although I don't know the structure of your movie, judging from the script you posted the player goes through that only once and also through i=1;
Everything is dependent on the onEnterFrame event, which fires continously and i=1; lies outside of that. Have you traced i? You need to trace everything to know what is going on.
FaceEraser
08-25-2005, 07:49 PM
After tracing and so on, I found the video times are loading correctly, but I still run into the same issue with the list.
The list and video advance to the second in the list when the first video has completed. When the second video completes, it plays itself again.
A link to the problem. I recommending advancing the video to the end when it's loaded (the first video is 15 minutes long.... and boring as hell):
http://www.dmeducate.com/pitt/
Here's the code starting at the load of the XML:
var vlist:XML = new XML();
vlist.load("xml/videos.xml");
vlist.ignoreWhite = true;
vlist.onLoad = function() {
var videos:Array = this.firstChild.childNodes;
for(i=0;i<videos.length;i++) {
listWin_mc.videoList.addItem(videos[i].attributes.desc, videos[i].attributes.url, videos[i].attributes.cue);
}
ns.play(listWin_mc.videoList.getItemAt(0).data);
listWin_mc.videoList.selectedIndex = 0;
}
var vidList:Object = new Object();
vidList.change = function() {
ns.play(listWin_mc.videoList.getItemAt(listWin_mc. videoList.selectedIndex).data);
}
listWin_mc.videoList.addEventListener("change", vidList);
var videoPlaying = 1; // replaced i as the variable name, it's being used elsewhere
onEnterFrame = function () {
getTotalTime();
totalPlayingTime = Math.round(duration);
currentPlayingTime = Math.round(ns.time);
if (currentPlayingTime == totalPlayingTime) {
delete onEnterFrame;
listWin_mc.videoList.selectedIndex = videoPlaying;
listWin_mc.videoList.addEventListener("change", vidList)
ns.play(listWin_mc.videoList.getItemAt(videoPlayin g++).data);
videoPlaying++;
if (videoPlaying>=videos.length) {
videoPlaying = 0;
}
}
}
Once again, thanks so much from a beginner!
cancerinform
08-25-2005, 07:52 PM
I have a look using a couple of short videos and try to fix the problem.
cancerinform
08-27-2005, 04:08 PM
Ok, I used videos provided by FlashMX2004 for this demonstration and it works perfectly. Here is the code.
var connection_nc:NetConnection = new NetConnection();
connection_nc.connect(null);
var ns:NetStream = new NetStream(connection_nc);
my_video.attachVideo(ns);
var count:Number = 0;
var myTime:Number;
//
myList.dataProvider = [{label:"Movie-1", data:"mark1.flv", myTime:18.733},
{label:"Movie-2", data:"main.flv", myTime:17.4},
{label:"Movie-3", data:"clock2.flv", myTime:12},
{label:"Movie-4", data:"clock2.flv", myTime:12}];
//
label1 = myList.getItemAt(count).data;
myList.selectedIndex = count;
ns.play(label1);
//
var vidList:Object = new Object();
vidList.change = function() {
myData = myList.getItemAt(myList.selectedIndex).data;
ns.play(myData);
};
myList.addEventListener("change", vidList);
//
playMovie();
function playMovie() {
count++;
_root.onEnterFrame = function() {
movieTime = myList.selectedItem.myTime;
if (ns.time == movieTime) {
my_video.clear();
delete _root.onEnterFrame;
myList.selectedIndex = count;
myData = myList.getItemAt(count).data;
ns.play(myData);
myList.addEventListener("change", vidList);
playMovie();
}
};
}
Now I tried to apply this to your code and this is what I came up with. First of all you need to measure the exact time of all your videos by using NetStream.time. Then you need to create another attribute where you store those data. Duration is not a method of the NetStream class but of the Sound class. So you cannot use the approach you have been using. Now here is the script I came up with but I cannot test it, that is your task now.
var connection_nc:NetConnection = new NetConnection();
connection_nc.connect(null);
var ns:NetStream = new NetStream(connection_nc);
my_video.attachVideo(ns);
//
var count:Number = 0;
var myData:String;
//
var vlist:XML = new XML();
vlist.load("xml/videos.xml");
vlist.ignoreWhite = true;
vlist.onLoad = function() {
var videos:Array = this.firstChild.childNodes;
for(i=0;i<videos.length;i++) {
listWin_mc.videoList.addItem(videos[i].attributes.desc, videos[i].attributes.url, videos[i].attributes.cue, videos[i].attributes.vidTime);
}
myData=listWin_mc.videoList.getItemAt(0).data;
ns.play(myData);
listWin_mc.videoList.selectedIndex = 0;
}
//
var vidList:Object = new Object();
vidList.change = function() {
myData = listWin_mc.videoList.getItemAt(listWin_mc.videoLis t.selectedIndex).data;
ns.play(myData);
}
listWin_mc.videoList.addEventListener("change", vidList);
//
playMovie();
function playMovie() {
count++;
_root.onEnterFrame = function() {
movieTime = listWin_mc.videoList.selectedItem.vidTime;
if (ns.time == movieTime) {
my_video.clear();
delete _root.onEnterFrame;
myList.selectedIndex = count;
myData = listWin_mc.videoList.getItemAt(count).data;
ns.play(myData);
listWin_mc.videoList.addEventListener("change", vidList);
playMovie();
}
};
}
If there are mistakes you need to change them of course.
Good luck :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.