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”.

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{}
facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Leave a Comment

Your email address will not be published. Required fields are marked *

Disclaimer: All published content views of author.