SoundItem Class

well we start with this Sound Util, just embed the SoundTransform and SoundChannel in one single Class and the fade ability (uses Tween Lite)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.grupow.media 
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
 
	import gs.TweenLite;
	import gs.easing.Linear;
 
	/**
	* ...
	* @author Raúl Uranga
	*/
	public class SoundItem extends Sound
	{
 
		private var _channel:SoundChannel
		private var _soundTransform:SoundTransform;
 
		public function SoundItem(stream:URLRequest = null,conext:SoundLoaderContext = null) {
 
			super(stream,conext);
 
			_soundTransform = new SoundTransform();
 
		}
 
		public function fade(volume:Number,ms:int = 1000,handler:Function = null):void
		{
			//
			if (volume < 0)
				volume = 0;
			//
			if (volume > 1)
				volume = 1;
			//	
			TweenLite.to(this, ms/1000, {volume:volume,ease:Linear.easeNone,onComplete:handler});
		}
 
		public override function play(startTime:Number = 0,loops:int = 0,sndTransform:SoundTransform = null):SoundChannel
		{
			if(sndTransform != null)
				_soundTransform = sndTransform; 
 
			_channel = super.play(startTime, loops, _soundTransform);
			return _channel;
		}
 
		public function stop():void
		{
			try {
				_channel.stop();
			}catch (e:*) {
				//trace("SoundItem Error: SoundChannel is not defined, you need to start playing");
			}
		}
 
 
		public function get volume():Number
		{ 
			return _soundTransform.volume;
		}
 
		public function set volume(value:Number):void 
		{
			_soundTransform.volume = value;
 
			try {
				_channel.soundTransform = _soundTransform;
			}catch (e:*) {
				//trace("SoundItem Error: SoundChannel is not defined, you need to start playing");
			}
		}
 
	}
}

code available at google code

cheers!

This entry was posted in AS3. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Trackback

  1. By 岛屿 » Blog Archive » SoundItem Class on 5 November 2008 at 6:33

    [...] This entry was posted in AS3. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*