Hi all,
New to the group, so hi!
Having an issue getting my JS to call ActionScript (AS) functions.
Scenario:
Have a page full of ringtones (15-25) and want to have the ability to
play a preview before purchase. Using a single file (ringtone.swf)
that has ActionScript that looks like this (I did not write this AS):
import flash.external.*;
ExternalInterface.addCallback("stopPlay", this, stopPlay);
ExternalInterface.addCallback("doPlay", this, doPlay);
_global.mySong = null;
_global.duration = null;
_global.position = null;
_global.playingID = null;
_global.streamingID = null;
_global.absoluteURL = "/url/where/ringtone/stored/ringtone.php?id=";
function formatTime(millis) {
var mins = Math.floor(millis / 60000);
var remaining = millis - (mins * 60000);
var secs = Math.floor(remaining / 1000);
if(secs < 10){
secs = '0' + secs;
}
return mins + ':' + secs;
}
var tooLong = 0;
function displayStreaming(mySound, where) {
if(tooLong++ > 3000) {
ExternalInterface.call('alert', "We're sorry, it appears that
this ringtone isn't available for listening at this time. Please try
again later.");
clearInterval(streamingID);
stopPlay();
tooLong = 0;
} else if(mySound.duration > 0) {
if(mySound.getBytesLoaded() == mySound.getBytesTotal()) {
clearInterval(streamingID);
ExternalInterface.call(isLoaded);
tooLong = 0;
}
where.text = formatTime(mySound.duration);
}
}
function displayPosition(mySound, where) {
if(mySound.position > 0){
if(mySound.position == mySound.duration) {
clearInterval(playingID);
}
where.text = formatTime(mySound.position);
}
}
function doPlay(mp3){
theSong = loc + absoluteURL + mp3;
clearInterval(streamingID);
clearInterval(playingID);
if(typeof mySong == "object") {
mySong.stop();
delete mySong;
}
duration.text = '0:00';
position.text = '0:00';
mySong = new Sound();
mySong.loadSound(theSong, true);
streamingID = setInterval(displayStreaming, 1, mySong, duration);
playingID = setInterval(displayPosition, 1, mySong, position);
mySong.onSoundComplete = function(){ stopPlay(); }
}
function stopPlay() {
duration.text = "0:00";
position.text = "0:00";
mySong.stop();
delete mySong;
clearInterval(streamingID);
clearInterval(playingID);
ExternalInterface.call(resetPlay);
}
function initSound() {
duration = _root.total_txt;
position = _root.time_txt;
}
initSound();
My JS looks like this:
//Embeded <object> ID
var embed_id = "ringtone";
//Global SWFObject vars
var player = "myContent";
var audio_file = "/assets/swf/ringtone.swf";
var player_width = '16';
var player_height = '16';
var flash_ver = "9.0.0";
var express_install = false;
var params = {allowScriptAccess: "always", swliveconnect: "true",
wmode: "transparent"};
var attributes = {id:embed_id,name:embed_id};
var call_backs = function(){};
//Gets current site URL
var base_url = "http://" + window.location.hostname + "/
product/";
function loadSWF(preview_id){
//SWFObject Vars
var flashvars = {loc: base_url};//, mp3: preview_id};
//var flashvars = {};
swfobject.embedSWF(audio_file, player, player_width, player_height,
flash_ver, express_install, flashvars, params, attributes,
call_backs);
}
//Determines the existence or absence of a previously embedded swf
file
function swfExists(obj_id){
var exists = false;
var object_tag = $("#"+obj_id);
if(object_tag && (object_tag.is("object") ||
object_tag.is("embed"))){
exists = true;
}
console.log("Exists: " +exists);
return exists;
}
$(document).ready(function(){
$(".ringtone-preview").on("click",function(e){
e.preventDefault();
var preview_id = $(this).attr('id');
console.log("Preview ID:" + preview_id);
//check to see if object exists
if(swfExists(embed_id)){
console.log("Secondary click");
swfobject.removeSWF(embed_id);
$("body").append("<div id='"+player+"'></div>");
loadSWF(preview_id);
}else{
console.log("Primary click");
loadSWF(preview_id);
}
var obj = document.getElementById(embed_id);
console.log(obj);
if (obj){
obj.doPlay(preview_id);
console.log("Playing");
}
});
});
I get an error when it gets to the "obj.doPlay(preview_id)" line that
reads:
obj.doPlay is not a function
If run loadSWF without the "mp3 : preview_id" piece commented out,
then it plays the ringtone. However, I want to be able to access the
stopPlay() function if the user fires the .ringtone-preview event
again.
I feel i'm almost there...just need access to the flash script.
Any help is appreciated.
--
You received this message because you are subscribed to the Google Groups
"SWFObject" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/swfobject?hl=en.