Solved

add new sound to next scene

Posted on 2011-03-17
4
744 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi designaire,
Do you need my help or you can close this question?
Thanks,
David
0
 

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now