I'm not sure if this is the proper place to post this, but since you
gave such great advice and know the history of the issue, I'm going to
post here.
Yesterday I said my issue was working, and it was...it went through
several testers and worked. However...something has gone amiss. I
can't put my finger on it...but here is the full code for both my JS
and AS followed by the problem:
Javascript:
//code environment subdomain: qa, prod, etc.
var env = '';
(window.location.href.indexOf(".qa.") > 0) ? env = ".qa" : env = "";
//initializes variable so that no preview is playing
var playing = false;
//used to determine what ringtone is currently playing, if any
var playing_id = 0;
//Embeded <object> ID
var embed_id = "ringtone";
//Global SWFObject vars
var player = "myContent";
var audio_file = "/assets/swf/audio.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 = "https://" + window.location.hostname + "/
ringer";
function playPreview(play,preview_id){
console.log("playPreview playing status: " + play);
var flashvars = {streamingID : preview_id};
var swf = document.getElementById(embed_id);
if(play == true){
$("#"+preview_id).removeClass("playPreview");
$("#"+preview_id).addClass("stopPreview");
console.log("Playing");
swf.doPlay(preview_id);
}else{
$("#"+preview_id).removeClass("stopPreview");
$("#"+preview_id).addClass("playPreview");
//stop SWF code
console.log("Stopping");
swf.stopPlay();
}
}
function resetPlay(playing_id){
$("#"+playing_id).addClass("playPreview");
$("#"+playing_id).removeClass("stopPreview");
console.log("Resetting");
}
function loadSWF(){
console.log(base_url);
var flashvars = {loc: base_url};//, mp3: preview_id};
swfobject.embedSWF(audio_file, player, player_width, player_height,
flash_ver, express_install, flashvars, params, attributes,
call_backs);
}
$(document).ready(function(){
//embed SWF into DOM; en lieu of static <script> in HTML doc
loadSWF();
$(".ringtone-preview").on("click",function(e){
e.preventDefault();
var preview_id = $(this).attr('id');
console.log("Preview ID:" + preview_id);
$("ul li").each(function(){
if($(this).hasClass('stopPreview')){
playing = true;
playing_id = $(this).attr('id');
console.log("Playing_id: " + playing_id);
return false;
}else{
playing = false;
}
});
console.log("Play Status: " + playing);
if(playing){
if(preview_id == playing_id){
playPreview(false,preview_id);
}else{
playPreview(false,playing_id);
playPreview(true,preview_id);
playing_id = $(this).attr('id');
}
}else{
playPreview(true,preview_id);
playing_id = $(this).attr('id');
}
});
});
Actionscript:
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.mp3ID = null;
_global.absoluteURL = "/preview.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;
mp3ID = 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", mp3ID);
}
function initSound() {
duration = _root.total_txt;
position = _root.time_txt;
}
initSound();
Expected Behavior:
When clicked, the .ringtone-preview sends an ID of an mp3 to the JS
which then sends it to the AS for playing. When clicked, an icon
changes from a "play" arrow to a "stop" square while the ringtone is
playing. After it plays, the "stop" arrow returns to the "play" arrow
indicating that it can be clicked again. The user can also click the
"stop" icon to stop the song before it is finished, in which case the
icon returns to a "play" arrow and the song stops.
Problem:
Two things are not happening that were happening last night:
1) In Firefox, the resetPlay function is not being called after the
song is done playing. I can click the stop button and it stop the
song, thus triggering the call to resetPlay. It does not, however, do
this when the song is done streaming, as it was doing last night. In
Chrome and IE, if I try this, it eventually (after about 30 seconds)
gives me a JS alert (the error above in the AS) and triggers the
resetPlay function.
2) Different things happen if I click the play button (wait for the
song to start playing) then click the stop button and then click the
play button again. In FF, doing this does not play the song a second
time (it plays, stops, but does not play again). In IE and Chrome, it
plays, stops and then it plays immediately after i click play the
second time (IE) or it waits about 15-20 seconds before it plays
(Chrome). In both cases, however, I get the error from the
displayStreaming function and THEN the resetPlay function is called
(changing the stop button to play button).
After I had the code working last night, I decided to clean up the
commented out code and get rid of things that I was using to test with
(functions, variables, etc) and clean it up for a more "plugin" like
feel for my client. After I had cleaned it up was when it stopped
working. Noting that their servers were really slow last night, I
decided to try to get it to work this morning after I got up. I undid
all my changes (thank the gods for CTRL + Z) and tried again...and
this is where I am.
Again, thanks in advance for any help you can provide. Nothing more
frustrating than having this work and then all the sudden not work.
On Nov 30, 5:04 pm, Aran Rhee <[email protected]> wrote:
> Glad to hear it is all working.
>
> Cheers,
> Aran
>
>
>
>
>
>
>
> On Wed, Nov 30, 2011 at 4:36 PM, Arknath <[email protected]> wrote:
> > That solved my problem! Thanks so much for the help. Everything is
> > playing and stopping with ease.
>
> > Really appreciate it.
>
> > On Nov 29, 5:54 pm, Aran Rhee <[email protected]> wrote:
> > > ExternalInterface.call(resetPlay); needs to be
>
> > > ExternalInterface.call("resetPlay");
>
> > > (the param is a String)
>
> > > See more here:
> >http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl...()
>
> > > A
>
> > > On Tue, Nov 29, 2011 at 5:19 PM, Arknath <[email protected]> wrote:
> > > > Thanks for the quick response! I think I've made some progress as to
> > > > what I'm doing here...learning as I go. :)
>
> > > > 1) From what it seems, the code is not designed to take the mp3
> > > > variable initially. With some slight changes to my own code, I've got
> > > > this calling the doPlay and stopPlay functions appropriately. (I'll
> > > > post new code below)
>
> > > > 2) This was my fault. I thought I needed to destroy the SWF and
> > > > recreate it every time. Silly me...I have since fixed that (as you'll
> > > > soon see)
>
> > > > 3) I did look at this more closely and this is something I was hoping
> > > > for from the resetPlay function. When the music stops (i.e. when
> > > > stopPlay has successfully executed) I want the JS to know that so that
> > > > I can reset a style to the original state (basically change a "stop"
> > > > button back to a "play" button). I thought the resetPlay callback was
> > > > going to do that for me, no such luck.
>
> > > > I have this working exactly the way I want except for that last issue
> > > > of the JS knowing when the song has stopped playing. Thanks for the
> > > > help, as your suggestions were correct. One more piece and I think i
> > > > have this thing licked. :)
>
> > > > On Nov 29, 4:03 pm, Aran Rhee <[email protected]> wrote:
> > > > > A couple of things which stand out:
>
> > > > > 1) I don't see anywhere in your pasted Flash code which deals with a
> > > > Flash
> > > > > var called "mp3", so I am unsure if there is more to the code or if
> > the
> > > > swf
> > > > > is not designed to take the mp3 var initially...
>
> > > > > 2) As you have a proper ExternalInterface method which loads / kills
> > > > > external sounds etc, you shouldn't ever need to destroy/recreate
> > your swf
> > > > > (unless you are using the same dom element to load other
> > swfs/content.
>
> > > > > 3) I would listen for the "resetPlay" function call in your html/js
> > code,
> > > > > as this tells you when the swf is loaded/external interface is all
> > set up
> > > > > etc. You are then safe to call doPlay(), stopPlay() etc on your swf.
>
> > > > > 4) Have a look at the EI example o the test suite to see how the code
> > > > > checks for the existence of functions / wires up the html buttons
> > etc:
>
> >http://www.bobbyvandersluis.com/swfobject/testsuite_2_2/test_dynamic_...
>
> > > > > with embed callback:
> > > >http://www.bobbyvandersluis.com/swfobject/testsuite_2_2/test_dynamic_.
> > ..
>
> > > > > Cheers,
> > > > > Aran
>
> > > > > On Tue, Nov 29, 2011 at 2:59 PM, Arknath <[email protected]> wrote:
> > > > > > 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
>
> ...
>
> read more »
--
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.