Here is another approach that seems to work, and which does not require any change to the flash file:
final JFlashPlayer flashPlayer = new JFlashPlayer();
FlashPluginOptions options = new FlashPluginOptions();
Map<String, String> keyToValueVariableMap = new HashMap<String, String>();
keyToValueVariableMap.put("testVar", "true");
options.setVariables(keyToValueVariableMap);
flashPlayer.load(xxxxxxxxx, options);
flashPlayer.runInSequence(new Runnable() {
public void run() {
new Thread() {
public void run() {
try {
sleep(2000);
} catch(Exception e) {}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.err.println("isLoaded: " + (flashPlayer.getVariable("testVar") != null));
}
});
}
}.start();
}
});
A few notes:
I spawn a new thread to sleep for 2 seconds. This leaves some time for the resource to be loaded. Of course, if the resource takes more than 2 seconds to be loaded, this would output false.
flashPlayer.getVariable("testVar") can be tested whenever you like, so you can create a "isLoaded" method that tests the presence of this variable.
I only tested under Windows with IE, but this is likely to work on other platforms as well. My test shows that if the file cannot be found, the variable is null.
-Christopher
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Your solution does solve the problem, but it requires every Flash application to implement this method, otherwise isInitialized would return false.
I am still trying to figure out a way to know what the Flash plugin has loaded. I have to look more thoroughly on the Internet to see if there is such API. The only thing I need is a public property of Flash that I could check from Javascript.
I don't want to have a half working solution as an official API though, so if you desperately need a temporary solution and are concerned about maintenance, you could do something similar to what you are doing but using a subclass of JFlashPlayer. Another way of propagating that info, if you are to add some code to the Flash file, is to send a command because the flash player accepts a listener to receive commands.
By-the-way, one potential problem with your implementation is if the page load status is 100% but if the Flash file takes time to load. If I am correct, 100% means that the plugin is created, but then the plugin starts downloading the Flash file and so on.
-Christopher
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1) Export a function called "isInitialized" from your Flash file like so:
ExternalInterface.addCallback("isInitialized", isInitialized);
function isInitialized():Boolean
{
return true;
}
2) Add a WebBrowserListener to detect failures:
Any ideas on how to internalize this into the JFlashPlayer API? Ideally this should be part of FlashPlayerListener.
Gili
Hi Gili,
Here is another approach that seems to work, and which does not require any change to the flash file:
final JFlashPlayer flashPlayer = new JFlashPlayer();
FlashPluginOptions options = new FlashPluginOptions();
Map<String, String> keyToValueVariableMap = new HashMap<String, String>();
keyToValueVariableMap.put("testVar", "true");
options.setVariables(keyToValueVariableMap);
flashPlayer.load(xxxxxxxxx, options);
flashPlayer.runInSequence(new Runnable() {
public void run() {
new Thread() {
public void run() {
try {
sleep(2000);
} catch(Exception e) {}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.err.println("isLoaded: " + (flashPlayer.getVariable("testVar") != null));
}
});
}
}.start();
}
});
A few notes:
I only tested under Windows with IE, but this is likely to work on other platforms as well. My test shows that if the file cannot be found, the variable is null.
-Christopher
Hi Gili,
Your solution does solve the problem, but it requires every Flash application to implement this method, otherwise isInitialized would return false.
I am still trying to figure out a way to know what the Flash plugin has loaded. I have to look more thoroughly on the Internet to see if there is such API. The only thing I need is a public property of Flash that I could check from Javascript.
I don't want to have a half working solution as an official API though, so if you desperately need a temporary solution and are concerned about maintenance, you could do something similar to what you are doing but using a subclass of JFlashPlayer. Another way of propagating that info, if you are to add some code to the Flash file, is to send a command because the flash player accepts a listener to receive commands.
By-the-way, one potential problem with your implementation is if the page load status is 100% but if the Flash file takes time to load. If I am correct, 100% means that the plugin is created, but then the plugin starts downloading the Flash file and so on.
-Christopher