Solved

CreateJS

Posted on 2013-11-18
12
311 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 42

Expert Comment

by:Rob Jurd, EE MVE
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
 
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 42

Expert Comment

by:Rob Jurd, EE MVE
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 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 42

Expert Comment

by:Rob Jurd, EE MVE
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 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39676147
http:#a39660613 this post shows you
0
 

Author Closing Comment

by:Angha110
ID: 39709644
Thank you
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to dynamically set the form action using jQuery.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now