Category Archives: Actionscript 3.0

A simple sound manager class

This is just a basic sound manager class, using this we can manage audio play/pause and stop. This class makes use of library class created in post “Creating asset library while working with AS3 projects”.

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
package com.sound
{
import assets.sound.SoundLibrary;

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.utils.getDefinitionByName;

public class SoundManager
{
private static var _instance:SoundManager;

private var _currentSound:Sound;
private var _soundChannel:SoundChannel;
private var _audioCurrentPosition:Number;

public function SoundManager(se:SingletonEnforcer)
{
_soundChannel = new SoundChannel();
}

public static function getInstance():SoundManager
{
if(!_instance )
_instance = new SoundManager(new SingletonEnforcer);
return _instance;
}

public function playAudio(audioName:String):void{
var tempClass:Class = SoundLibrary.getAssetClass(audioName);
var smallSound:Sound = new tempClass() as Sound;
if(smallSound){
_soundChannel.stop();
}
_currentSound = smallSound;
_soundChannel = smallSound.play();
}

public function audioPause():void{
_audioCurrentPosition = _soundChannel.position;
_soundChannel.stop();
}

public function audioPauseContinue():void{
_currentSound.play(_audioCurrentPosition);
}
}
}

class SingletonEnforcer{}