Saturday, February 06, 2010

Embeding Flash in Facebook Dialog box

UPDATE: Some of the techniques below are no longer working in Facebook. Please find an updated technique here: Updated: Embeding Flash in Facebook Dialog Box

Ran into more work than I expected on this, and didn't find much in the way of resources out there to help.

Here's the scenario: on a Facebook page tab (profile tab) want to have links that open a dialog box with a youtube video. In my case its a nice image map (css driven) of images that open the video the image represents.

Primary challenges:
fb:swf fbml must be used. Actually there is now a createFBMLObject() in FBJS and the only object it will create is fb:swf but its in beta and there isn't much documentation yet.

Attempt #1:
After much research and trial and error I got this as a workable solution:
use fb:dialog with the fb:swf as the content block
<a title="Pop up video" href="#"  clicktoshowdialog="tubeIt1"></a>
<fb:dialog id="tubeIt1" cancel-button="true">
<fb:dialog-title>Luxury</fb:dialog-title> 
<fb:dialog-content>
<fb:swf 
swfsrc='http://www.youtube.com/v/PQHPYelqr0E&hl=en_US&fs=1&'
imgsrc='http://www.someurl.com/static/images/PorkAndBeans.jpg' 
width='430' height='295' />
<span>Pork and beans!</span>
</fb:dialog-content>
<fb:dialog-button type="button" value="close" close_dialog="true" /> 
</fb:dialog>



This solution works well enough and is easy, however it introduces a problem - if a user opens the dialog, then closes it, then opens it again the video can no longer be played (the static image is not clickable).

Final solution:
Final solution I landed on was to have fbjs create the dialog and just store the fb:swf in a fb:js-string. This removes the need to click through the static image to play the flash in the dialog box.
However it didn't work the second time the dialog was opened as the flash had already been injected. The workaround to that turned out to be replacing a marker in the dialog with the fb:js-string variable.

Here it is:
<script language="javascript">
var dialogCounter = 0;
var dialogActive;


function showVideo(title, video) {
if(dialogCounter > 0)  return false; //don't allow more than one instance of this dialog
else {

dialogCounter += 1;

dialogActive = new Dialog();

dialogActive.showMessage(title + dialogCounter, dialogText); 

dialogActive.onconfirm = function() { dialogCounter = 0; }

document.getElementById('dialog_content').setInnerFBML(video);

return false;

}
}



</script>

<fb:js-string var="dialogText">   
<div id="dialog_content">
Marker to be replaced
</div>  
</fb:js-string> 

<a title="Pork and Beans" href="#" onclick="return showVideo('Pork and Beans', tubeIt1)"></a>

<fb:js-string var="tubeIt1">
<fb:swf 
swfsrc='http://www.youtube.com/v/PQHPYelqr0E&hl=en_US&fs=1&'
imgsrc='http://www.someurl.com/static/images/PorkandBeans.jpg' 
width='430' height='295' />
<span>Pork and Beans!</span>
</fb:js-string>


The other annoyance was that the dialogs stack on each other causing the user to have to "ok" each one individually if they open more than one. Introducing the dialogCounter variable prevents any new dialogs from being opened if one is already open.

Hope this saves someone else some time!

9 comments:

Anonymous said...

Your prior experience, throughts, and willingness to share - saved the day.

Thanks!

Rob Fuller said...

Thank you! I'm glad I was able to help!

Anonymous said...

FYI If You want to save even more time put an iframe in fb:js-string

Stephen Barker said...

Fantastic work! Could have used the guidance sooner if I'd found it earlier, but the tip for allowing the video to be loaded a second time really saved me a lot of frustration!

Thanks!

Anonymous said...

Thank you very very much! ... you saved me from an awful night :B

masterb said...

Your workaround is not working...
second time i click the button (after i click okay) the video is not loaded...

Rob Fuller said...

odd, its working for me. If you post the code you are using I'll take a look.

Melisa said...

Hello, I tried your code and it works on first dialog open but when I close the dialog and open it again, the fb swf tag won't render. Do you have any idea for this?

Sorry for the problem and thank you very much

Rob Fuller said...

Found that for new tabs/profile boxes this technique no longer worked - please see the updated post: http://robfuller.blogspot.com/2010/09/updated-embeding-flash-in-facebook.html