Solved

add new sound to next scene

Posted on 2011-03-17
4
751 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
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
dgofman earned 250 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
YouTube Link 10 359
How to display a webpage in a flash 10 361
adobe flash plugin has crashed while listening to pandora 2 71
word is recognized as adobe and adobe as IE files 14 41
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…

821 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