Solved

CreateJS

Posted on 2013-11-18
12
320 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

763 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