package com inx utilities import flash events Event import flash event

  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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.inx.utilities {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.ProgressEvent;
import flash.events.TimerEvent;
import flash.media.ID3Info;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;
import flash.utils.Timer;
public class SoundFactory extends EventDispatcher {
public static const LOADING:String = "loading";
private static var _instance:SoundFactory;
private var _sound:Sound;
private var _urlRequest:URLRequest;
private var _isPlaying:Boolean;
private var _isPaused:Boolean;
private var _currTrack:SoundChannel;
private var _percentLoaded:Number;
private var _playHeadPositionTimer:Timer;
private var _playHeadPosition:Number;
private var _id3:ID3Info;
public function SoundFactory() {}
public static function getInstance():SoundFactory {
if(_instance == null) {
_instance = new SoundFactory();
}
return _instance;
}
public function play():void {
if(!_isPlaying && !_isPaused) {
_sound = new Sound();
_sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
_sound.load(_urlRequest);
_id3 = _sound.id3;
_currTrack = _sound.play();
_currTrack.addEventListener(Event.SOUND_COMPLETE, playbackCompleteHandler);
_isPlaying = true;
_playHeadPositionTimer = new Timer(50);
_playHeadPositionTimer.addEventListener(TimerEvent.TIMER, playbackTimeHandler);
_playHeadPositionTimer.start();
} else if(_isPaused) {
_currTrack = _sound.play(_playHeadPosition);
_isPaused = false;
_isPlaying = true;
}
}
public function pause():void {
if(_isPlaying) {
_isPaused = true;
_isPlaying = false;
_currTrack.stop();
} else if(_isPaused) {
play();
}
}
public function stop():void {
if(_isPlaying || _isPaused) {
_currTrack.stop();
_isPlaying = false;
_isPaused = false;
if(_percentLoaded < 100) {
_sound.close();
}
}
}
[Bindable(event="loading")]
public function get percentLoaded():String {
return _percentLoaded == 100 ? "100" : _percentLoaded.toFixed(1);
}
public function get isPlaying():Boolean {
return _isPlaying;
}
public function get id3():ID3Info {
return _id3;
}
public function get currentTrack():SoundChannel {
return _currTrack;
}
public function set urlRequest(urlRequest:String):void {
_urlRequest = new URLRequest(urlRequest);
}
public function progressHandler(event:ProgressEvent):void {
var bytesTotal:uint = event.bytesTotal;
var bytesLoaded:uint = event.bytesLoaded;
_percentLoaded = (bytesLoaded / bytesTotal) * 100;
var e:Event = new Event(SoundFactory.LOADING);
dispatchEvent(e);
}
public function playbackCompleteHandler(event:Event):void {
_playHeadPositionTimer.stop();
}
public function playbackTimeHandler(event:Event):void {
_playHeadPosition = _currTrack.position;
}
}
}