Solved

CreateJS

Posted on 2013-11-18
12
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
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 250 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 250 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

695 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