Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

add new sound to next scene

Posted on 2011-03-17
4
Medium Priority
?
765 Views
Last Modified: 2012-05-11
I'm using as3 and I have sound running using the page below. When it finishes it goes to the next scene. Is there anyway I can change the sound on the next page without repeating all the code with next functions etc... It also has stop, rewind and a next and previous button.
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
import flash.media.SoundTransform;

//btn_stop.addEventListener(MouseEvent.CLICK, stopScene);

var myChannel:SoundChannel = new SoundChannel();
var soundReq:URLRequest = new URLRequest("test_05.mp3");
var mySound:Sound = new Sound();
mySound.load(soundReq);
 
mySound.addEventListener(Event.COMPLETE, onComplete);

function onComplete (event:Event):void {
      var myChannel:SoundChannel = new SoundChannel();
      myChannel = mySound.play();
      myChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished);
}
 
function soundFinished (event:Event):void {
      myChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished);
      trace("my sound finished!");
	  gotoAndPlay(2, "Scene 2"); 
} 

var myTransform = new SoundTransform();
var lastPosition:Number = 0;
//myChannel = mySound.play();
myTransform.volume = 0.5;
myChannel.soundTransform = myTransform;

btn_rewind.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void{
	myChannel.stop();
	myChannel = mySound.play();
	myTransform.volume = 0.5;
}

btn_mute.addEventListener(MouseEvent.CLICK, onClickMuteOn);
function onClickMuteOn(e:MouseEvent):void{
	lastPosition = myChannel.position;
	myChannel.stop();
}

btn_playsound.addEventListener(MouseEvent.CLICK, onClickMuteOff);
function onClickMuteOff(e:MouseEvent):void{
		myChannel = mySound.play(lastPosition);
}

btn_back.addEventListener(MouseEvent.CLICK, goBack);
btn_next.addEventListener(MouseEvent.CLICK, goForward);

function goBack(event:MouseEvent):void {
    prevScene();
}

function goForward(event:MouseEvent):void {
    nextScene();
}

function stopScene(event:MouseEvent):void {
    stop();
}

Open in new window

0
Comment
Question by:designaire
[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
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
dgofman earned 1000 total points
ID: 35160709
The best way to create AS3 Singelton class

package
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.media.SoundTransform;

	public class SoundClass extends EventDispatcher
	{
		private static var _instance:SoundClass;

		private var myChannel:SoundChannel;
		private var mySound:Sound;
		private var myTransform:SoundTransform;
		private var lastPosition:Number;

		/*private*/ function SoundClass(){
			myChannel = new SoundChannel();
			myTransform = new SoundTransform();
			myTransform.volume = 0.5;
			myChannel.soundTransform = myTransform;
			mySound = new Sound();
			mySound.addEventListener(Event.COMPLETE, play);
		}
		
		public static function get instance():SoundClass{
			if(_instance == null)
				_instance = new SoundClass();
			return _instance;
		}

		public function load(url:String):void{
			var soundReq:URLRequest = new URLRequest(url);
			lastPosition = 0;
			mySound.load(soundReq);
		}

		public function play(event:Event=null):void {
			myChannel = mySound.play(lastPosition);
			myChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished);
		}
		
		public function stop():void{
			lastPosition = myChannel.position;
			myChannel.stop();
		}

		public function set volume(volume:Number):void{
			myTransform.volume = volume;
		}

		public function soundFinished (event:Event):void {
			myChannel.removeEventListener(Event.SOUND_COMPLETE, soundFinished);
			dispatchEvent(event);
		}
	}
}

Open in new window



Now you need just use any places in your code

import SoundClass;

import flash.events.MouseEvent;

SoundClass.instance.addEventListener(Event.COMPLETE, soundFinished);
SoundClass.instance.load("http://examples.adobe.com/flex2/inproduct/sdk/explorer/effects/assets/jazz.mp3");

function soundFinished (event:Event):void {
      SoundClass.instance.removeEventListener(Event.SOUND_COMPLETE, soundFinished);
      trace("my sound finished!");
	  gotoAndPlay(2, "Scene 2"); 
} 

function onClickPlay(e:MouseEvent):void{
	SoundClass.instance.stop();
	SoundClass.instance.play();
	SoundClass.instance.volume = 0.5;
}

function onClickMuteOn(e:MouseEvent):void{
	SoundClass.instance.stop();
}

function onClickMuteOff(e:MouseEvent):void{
	SoundClass.instance.play();
}

btn_rewind.addEventListener(MouseEvent.CLICK, onClickPlay);
btn_mute.addEventListener(MouseEvent.CLICK, onClickMuteOn);
btn_playsound.addEventListener(MouseEvent.CLICK, onClickMuteOff);
btn_back.addEventListener(MouseEvent.CLICK, goBack);
btn_next.addEventListener(MouseEvent.CLICK, goForward);

function goBack(event:MouseEvent):void {
    prevScene();
}

function goForward(event:MouseEvent):void {
    nextScene();
}

function stopScene(event:MouseEvent):void {
    stop();
} 

Open in new window

0
 

Author Comment

by:designaire
ID: 35161471
Thanks I wish I could understand it all. I know something about classes but perhaps not enough. The rewind button and the continue to the next button didn't seem to work however. I'll look at it more tormorrow and if you see anything that would cause that let me know (I will award you points reguardless)
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35171152
Hi designaire,
Do you need my help or you can close this question?
Thanks,
David
0
 

Author Closing Comment

by:designaire
ID: 35172615
Thanks, I'll work on it! I'll post again if I need it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

604 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