Solved

CreateJS

Posted on 2013-11-18
12
323 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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.

737 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