Ok, so I have this FLV player that pulls all of the data (poster frame url, flv url, etc…) from an external xml file “prefs.xml” that’s in the same root as the swf. That all works nicely.
My goal is:
I need to be able to house the master version of the swf in one location (www.joeFREELANCE.com/SWF/player.swf) and have html pages all over the place ([URL=“http://www.yoursite.com/testpage.html”]www.yoursite.com/testpage.html, [URL=“http://www.yourmammassite.com/datpage.html”]www.yourmammassite.com/datpage.html, etc…) all link to that same swf to deliver different videos, depending on the configuration of their individual prefs.xml file in the root of the linking html page.
Does that make sense? I know cross-domain policies come into effect, and I actually have one hard-coded into the swf, and linked from the prefs.xml file so you can change the url of that document.
Ultimately, everything works as long as the swf is in the same root as the html page and the prefs.xml file, but I need it to work when the swf is in a different location.
Here’s my code:
[SIZE=5]AS3 PLAYER CODE:[/SIZE]
[SIZE=5][/SIZE]
import joe.project.utilities.xmlPrefs;
import flash.system.SecurityDomain;
import fl.video.VideoEvent;
import fl.video.CuePointType;
import fl.video.VideoEvent;
import fl.video.MetadataEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.external.ExternalInterface;
import fl.video.FLVPlaybackCaptioning;
import fl.video.CaptionChangeEvent;
import flash.display.Sprite;
import flash.events.MouseEvent;
//START check for screen reader, and sets a variable that can be used to branch and can be toggled on and off
var readerActive:String = "";
if(readerActive == "") {
readerActive = Accessibility.active?"active":"inactive";
}
//END check for screen reader, and sets a variable that can be used to branch and can be toggled on and off
//START variable to set the stage for loading the prefs.xml document
var Loadedxml:XML;
var urlLoader:URLLoader = new URLLoader();
//END variable to set the stage for loading the prefs.xml document
//START so that the xmlPrefs class will always look to the same directory as the swf file and not be based on the URL of the containing page.
var myLocation:String = stage.loaderInfo.url;
var myLocation_Arr:Array = myLocation.split("/");
var i:uint = 0;
myLocation = "";
for (i = 0; i < myLocation_Arr.length -1; i++) {
myLocation += myLocation_Arr* + "/";
}
var myHTMLLocation = "";
var prefName = "prefs.xml";
//END so that the xmlPrefs class will always look to the same directory as the swf file and not be based on the URL of the containing page.
//START creates variables that info from prefs.xml get loaded into
//var myBug:Loader = new Loader();
var myPoster:Loader = new Loader();
var prefVideoTitle:String;
var prefVideoSrc:String;
var prefVideoThumb:String;
var prefVideoCap:String;
//START loading prefs
trace('loading prefs');
var prefs:xmlPrefs = new xmlPrefs(myLocation, myHTMLLocation, "html", prefName);
prefs.addEventListener("prefsWritten", setPrefs)
//END loading prefs
var videoTitle:String = prefs.prefVideoTitle;
var videoDescription:String = prefs.prefVideoDescription;
var videoSrc:String = prefs.prefVideoSrc;
var videoThumb:String = prefs.prefVideoThumb;
var videoCap:String = prefs.prefVideoCap;
var videoBug:String = prefs.prefVideoBug;
var domainPolicy:String = prefs.prefDomainPolicy;
//END creates variables that info from prefs.xml get loaded into
Security.loadPolicyFile("http://www.joeFREELANCE.com/SWF/cross-domain-policy.xml");
//START function that sets prefs loaded from prefs xml document
function setPrefs(e:Event):void {
trace('setting prefs');
videoTitle = prefs.prefVideoTitle.toString();
videoDescription = prefs.prefVideoDescription.toString();
videoSrc = prefs.prefVideoSrc;
videoThumb = prefs.prefVideoThumb;
videoCap = prefs.prefVideoCap;
videoBug = prefs.prefVideoBug;
domainPolicy = prefs.prefDomainPolicy;
//START Use the domain policy's url from the prefs.xml document
Security.loadPolicyFile(domainPolicy);
//END Use the domain policy's url from the prefs.xml document
trace('all prefs set');
trace('processing XML');
prefVideoTitle = prefs.prefVideoTitle;
prefVideoSrc = prefs.prefVideoSrc;
prefVideoThumb = prefs.prefVideoThumb;
prefVideoCap = prefs.prefVideoCap;
myVideo.source=prefVideoSrc;
captioning.source=prefVideoCap;
descTitle.text=prefVideoTitle;
var imageURL:String = prefVideoThumb;
trace("the image loading is: ", imageURL);
var imageURLRequest = new URLRequest(imageURL);
myPoster.load(imageURLRequest);
myPoster.x = 0;
myPoster.y = 0;
addChild(myPoster);
addChild(myPoster);
//var itemBug:String = item.bug.toString();
//var bugURL:String = itemBug;
//var bugURLRequest = new URLRequest(bugURL);
//myBug.load(bugURLRequest);
//myBug.x = 5;
//myBug.y = 320;
//myBug.alpha = .5;
//addChild(myBug);
}
//END function that sets prefs loaded from prefs xml document
//START defining buttons and associating them with the video component
myVideo.playButton = Player;
myVideo.stopButton = Stopper;
//END defining buttons and associating them with the video component
//START Event listeners
Player.addEventListener(KeyboardEvent.KEY_DOWN, playMovie);
Stopper.addEventListener(KeyboardEvent.KEY_DOWN, pauseMovie);
CCer.addEventListener(KeyboardEvent.KEY_DOWN, ccControl);
myPoster.addEventListener(MouseEvent.CLICK, playMovieClick);
myPoster.addEventListener(KeyboardEvent.KEY_DOWN, playMovie);
myVideo.addEventListener(VideoEvent.PLAYING_STATE_ENTERED, hidePosterFrame);
myVideo.addEventListener(VideoEvent.COMPLETE, showPosterFrame);
//END Event listeners
//START button function events
function showPosterFrame(event:Event):void {
myPoster.visible = true;
}
function hidePosterFrame(event:Event):void {
myPoster.visible = false;
}
function playMovie(event:KeyboardEvent):void {
if(event.keyCode == 13 || event.keyCode == 32) {
// need to trap for the enter key
myVideo.play();
}
}
function ccControl(event:KeyboardEvent):void {
if(event.keyCode == 13 || event.keyCode == 32) {
// need to trap for the enter key
if (captioning.showCaptions == false) {
captioning.showCaptions = true;
} else {
captioning.showCaptions = false;
}
}
}
function playMovieClick(event:MouseEvent):void {
myVideo.play();
}
function pauseMovie(event:KeyboardEvent):void {
if(event.keyCode == 13 || event.keyCode == 32) {
// 13 is Enter key
myVideo.pause();
}
}
//END button function events
I’m pretty sure the problem has to do with this chuck of code (from the above code):
//START so that the xmlPrefs class will always look to the same directory as the swf file and not be based on the URL of the containing page.
var myLocation:String = stage.loaderInfo.url;
var myLocation_Arr:Array = myLocation.split("/");
var i:uint = 0;
myLocation = "";
for (i = 0; i < myLocation_Arr.length -1; i++) {
myLocation += myLocation_Arr* + "/";
}
var myHTMLLocation = "";
var prefName = "prefs.xml";
//END so that the xmlPrefs class will always look to the same directory as the swf file and not be based on the URL of the containing page.
[SIZE=5]And here’s the xmlPrefs.as Code (for loading the prefs.xml file)[/SIZE]
package joe.projects.utilities {
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;
/**
* RSSParser includes methods for
* converting RSS XML data into HTML text.
*/
public class xmlPrefs extends EventDispatcher {
/**
* The XML object containing the source RSS data
*/
public var prefsXML:XML;
public var swfPath:String = '';
/**
* The string that will contain the converted HTML version of the RSS topic data.
*/
public var prefVideoTitle:String;
public var prefVideoDescription:String;
public var prefVideoSrc:String;
public var prefVideoThumb:String;
public var prefVideoCap:String;
public var prefVideoBug:String;
public var prefBackgroundImage:String;
public var prefBackgroundColor:String;
public var prefDomainPolicy:String = "http://www.joeFREELANCE.com/SWF/cross-domain-policy.xml";
/**
* Used to load RSS data.
*/
private var myLoader:URLLoader;
/**
* An event used to signal that the HTML version of the RSS data has been written.
*/
private var prefsWritten:Event;
/**
* Initiates loading of the RSS data.
*/
public function xmlPrefs(path:String, htmlPath:String = "", pathType:String = "html", fileName:String = "prefs.xml") {
if (pathType == "html") {
swfPath == htmlPath;
} else {
swfPath = path;
}
var [url:String](http://www.kirupa.com/forum/String) = swfPath + fileName;
trace(url);
var prefsXMLURL:URLRequest = new URLRequest(url);
trace(prefsXMLURL.url);
myLoader = new URLLoader(prefsXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
public function xmlLoaded(evtObj:Event):void {
trace("reading prefs");
prefsXML = XML(myLoader.data);
//var outXML:XMLList = new XMLList();
/* The source RSS data may or may not use a namespace to define
* its content.
*/
if (prefsXML.namespace("") != undefined) {
default xml namespace = prefsXML.namespace("");
}
XML.prettyPrinting = false;
for each (var pref:XML in prefsXML) {
prefVideoTitle = pref.videoTitle.toString();
prefVideoDescription = pref.videoDescription.toString();
prefVideoSrc = pref.videoSrc.toString();
prefVideoThumb = pref.videoThumb.toString();
prefVideoCap = pref.videoCap.toString();
prefVideoBug = pref.videoBug.toString();
prefDomainPolicy = pref.domainPolicy.toString();
}
prefsWritten = new Event("prefsWritten", true);
dispatchEvent(prefsWritten);
}
}
}
[SIZE=5]Last, here’s the cross-domain policy document that I’m using:[/SIZE]
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy
SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
[SIZE=5]Prefs.XML[/SIZE]
<?xml version="1.0" encoding="utf-8"?>
<pref>
<videoTitle>Here's the Video Title</videoTitle>
<videoDescription>Here's the Video Description</videoDescription>
<videoCap>sampleCaption.xml</videoCap>
<videoSrc>assets/sampleVideo.flv</videoSrc>
<videoThumb>sampleImage.jpg</videoThumb>
<videoBug>bug_logo.png</videoBug>
<domainPolicy>http://www.joeFREELANCE.com/SWF/cross-domain-policy.xml</domainPolicy>
</pref>
Hopefully someone out there can help - this is driving me crazy. Everything works, I just can’t get the swf to look for the prefs.xml file at the same level as the html page it’s embedded on.
Thanks,
Joe
joe@joeFREELANCE.com