Solved

add new sound to next scene

Posted on 2011-03-17
4
756 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

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

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
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 how to record live broadcast.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

809 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