?
Solved

add new sound to next scene

Posted on 2011-03-17
4
Medium Priority
?
762 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
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.
Suggested Courses

777 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