问题
I have embedded a single swf three times named as video.swf.
The swfs names are video1 , video2 and video3.
If I play a swf, I wants to get the currently playing swf's name?
Is it possible ?
I'm using javascript for communication.
回答1:
I had to do something very similar for work on a project for Swatch/MTV (having multiple embedded players on a page and playing only one clip at a time (playing a different clip, would pause others, etc.) e.g.
var vids = ['video/file/72066f40bfcaea46e10460585b4e4bcb.mp4','video/file/3d5db6b87f9cdacb016c9c55afed1e08.mp4','video/file/c18b04a1a548cbf20609de70a106d7cc.mp4','video/file/4568a11f3f6a7ff467a85fefe2ac08e6.mp4','video/file/b91081d37a81692194c0e34580958c51.mp4']; for(var i = 0 ; i < vids.length; i++){
var flashvars = {};
flashvars.video_url = 'http://www.swatchmtvplayground.com/'+vids[i];
flashvars.video_id = i;
flashvars.locale = "gb";
flashvars.skin = 'upperBackground:0xf8c3c4,lowerBackground:0xe2e2e2,generalControls:0x000000,slider:0xb58f8f,progress:0xe2e2e2';
var params = {};
var attributes = {};
attributes.id = "mediaplayer"+i;
so = swfobject.embedSWF("http://www.swatchmtvplayground.com/flash/mediaplayer/mediaplayer.swf", "mediaplayer"+i, "578", "345", "10.0.0", false, flashvars, params, attributes);
}
function pauseAllPlayers(exceptThisOne){
for(var i = 0 ; i < vids.length ; i++) if(exceptThisOne != "mediaplayer"+i) document.getElementById("mediaplayer"+i).pause();
}
to get the id I've used a neat little trick I didn't previously know about (executing JS created with actionscript) using Zeh Fernando's excellent guide: Getting the SWF’s HTML object/embed id from within the Flash movie itself
HTH
回答2:
If you're using the same swf file three times you'd have to pass in a flash var to let the swf know which instance it is (video1, video2, or video3). Then when a video.swf instance starts playing use AS3's ExternalInterface to call JavaScript and mark that swf instance as the one currently playing.
Using SWFObject to embed the swfs in the page you can set the flashvars in JavaScript like this:
var flashvars1 = {
name: "video1",
};
swfobject.embedSWF("video1.swf", "flashContent1", "640", "480", "10.0.0", false, flashvars1, {}, {});
var flashvars2 = {
name: "video2",
};
swfobject.embedSWF("video2.swf", "flashContent2", "640", "480", "10.0.0", false, flashvars2, {}, {});
var flashvars3 = {
name: "video3",
};
swfobject.embedSWF("video3.swf", "flashContent3", "640", "480", "10.0.0", false, flashvars3, {}, {});
Within each swf you'll now have a 'name' var that can be accessed through LoaderInfo:
var name:String = LoaderInfo(this.root.loaderInfo).parameters.name;
And you call ExternalInterface from Flash like so:
ExternalInterface.call( "videoPlaying", name );
This would call a JavaScript function called 'videoPlaying' with the name as the argument:
function videoPlaying(name) {
// do something with the name arg
}
来源:https://stackoverflow.com/questions/7011043/how-to-get-the-swf-name-using-javascript