[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CreateJS

Posted on 2013-11-18
12
Medium Priority
?
344 Views
Last Modified: 2013-12-10
I have this HTML5 file which plays the audio and I am trying to add a button to my canvas and have it pause/resume the audio.

I have this code (which works fine) and it start the music.:

this.frame_0 = function() {
		//playSound("iStock_000022177119_Snowwav",-1);
		 var myInstance = createjs.Sound.createInstance("myAudio");
		 myInstance.play(createjs.Sound.INTERRUPT_ANY);
	}

Open in new window


so my two question is :
1. How to add two objects to my canvas which I can use it as pause/play buttons.
2. How to add a "pauseBtton.click" to my code above.

I tried this: and it did not work.
$("myPause").click(function() {
		myInstance.pause();
		
	});

Open in new window

0
Comment
Question by:Angha110
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 39658742
Take a look at what i did here. This also shows how to add audio as well as pause/play
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28233480.html
0
 

Author Comment

by:Angha110
ID: 39659086
But I am using createjs. I want the related solution for createJS.
Thank you for the link.
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39659206
try it like this. the pause method has to be in scope with the myInstance variable.

this.frame_0 = function() {
		
		var myInstance = createjs.Sound.play("myAssetPath/mySrcFile.mp3");

                $("myPause").click(function() {
		     myInstance.pause();
		});
	}

Open in new window

If this doesn't work, post the complete code, or a link to the page.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 39660241
What about going a step further and passing myInstance as a function parameter?

this.frame_0 = function() {
		
		var myInstance = createjs.Sound.play("myAssetPath/mySrcFile.mp3");

                $("myPause").click(function(myInstance) {
		     myInstance.pause();
		});
	} 

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 39660601
You can pause like this:

this.frame_0 = function() {
            //playSound("iStock_000022177119_Snowwav",-1);
             var myInstance = createjs.Sound.createInstance("myAudio");
             var myPlayItem = myInstance.play(createjs.Sound.INTERRUPT_ANY);
      }

$("myPause").click(function() {
            myPlayItem.pause();
            
      });
0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 1000 total points
ID: 39660613
The play() function returns a SoundInstance Class that has both the pause and resume methods

http://www.createjs.com/Docs/SoundJS/classes/SoundInstance.html#method_resume

I would take it one step further for your resume button:

$("myPlay").click(function() {
            myPlayItem.resume();
            
      });

Open in new window

This could just be the same button and you could toggle it's state:

var isPaused = false;
$("myButton").click(function() {
    if (isPaused) {
        myPlayItem.resume();
    }
    else {
        myPlayItem.pause();
   }        
      });

Open in new window

0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39660961
as long as you keep declaring the instance inside the this.frame_0 function, it will have that function's scope, and you will not be able to access it outside of that function.

Either declare the variable outside the function, or access its methods inside the function.

tagit, your myPlayItem in the click functions is a global. it does not reference the var myPlayItem in the this.frame_0. It will  not work.
0
 
LVL 43

Expert Comment

by:Rob
ID: 39660973
@kozaiwaniec - well spotted.  indeed you're correct.  

The variable needs to have global scope or the same scope as the function as @kozaiwaniec has said.

eg

var myPlayItem;
this.frame_0 = function() {
            //playSound("iStock_000022177119_Snowwav",-1);
             var myInstance = createjs.Sound.createInstance("myAudio");
             myPlayItem = myInstance.play(createjs.Sound.INTERRUPT_ANY);
      }

Open in new window

0
 

Author Comment

by:Angha110
ID: 39676105
I am kind of confused now.

This does play the audio.
var myPlayItem;
this.frame_0 = function() {
            //playSound("iStock_000022177119_Snowwav",-1);
             var myInstance = createjs.Sound.createInstance("myAudio");
             myPlayItem = myInstance.play(createjs.Sound.INTERRUPT_ANY);
      }


Now, where do I add the pause?
0
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 1000 total points
ID: 39676114
now that you'v exposed the myPlayItem variable, you can put the pause method anywhere within the scope of that variable, for example:

var myPlayItem;
this.frame_0 = function() {
            //playSound("iStock_000022177119_Snowwav",-1);
             var myInstance = createjs.Sound.createInstance("myAudio");
             myPlayItem = myInstance.play(createjs.Sound.INTERRUPT_ANY);
      }
var isPaused = false;
$("myButton").click(function() {
    if (isPaused) {
        myPlayItem.resume();
    }
    else {
        myPlayItem.pause();
   }        
      });
0
 
LVL 43

Expert Comment

by:Rob
ID: 39676147
http:#a39660613 this post shows you
0
 

Author Closing Comment

by:Angha110
ID: 39709644
Thank you
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question