-
Notifications
You must be signed in to change notification settings - Fork 17
/
Mp3Player.hx
157 lines (140 loc) · 4.15 KB
/
Mp3Player.hx
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
////////////////////////////////////////////////////////////////////////////////
//
// Muses Radio Player - Radio Streaming player written in Haxe.
//
// Copyright (C) 2009-2012 Federico Bricker
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// This Project was initially based on FOggPlayer by Bill Farmer. So
// my special thanks to him! :)
//
// Federico Bricker f bricker [at] gmail [dot] com.
//
////////////////////////////////////////////////////////////////////////////////
import flash.errors.EvalError;
import flash.utils.Timer;
class Mp3Player extends Player {
var sound : flash.media.Sound;
var channel : flash.media.SoundChannel;
var oldPlayingChannel : flash.media.SoundChannel;
var oldPlayingPosition : Float;
var switchChannelTimer : Timer;
public function new(ui:UI,url:String,tracker:Tracker,fallbackUrl:String,introUrl:String,reconnectTime:Int){
super(ui,url,tracker,fallbackUrl,introUrl,reconnectTime);
switchChannelTimer = new Timer(100, 1);
switchChannelTimer.addEventListener(flash.events.TimerEvent.TIMER, switchChannels);
}
override function createSoundObject(){
sound=new flash.media.Sound();
soundObject=sound;
}
override function closeSound() {
sound.close();
Reflect.deleteField(this,'sound');
sound = null;
soundObject = null;
}
override function loadSound(request:flash.net.URLRequest){
var sc:flash.media.SoundLoaderContext=new flash.media.SoundLoaderContext(buffering*1000,false); // set the buffering time in seconds.
sound.load(request,sc);
}
override function startSound() {
channel = sound.play();
trafficControl();
}
override function soundLength():Float{
return sound.length;
}
override function getProgress() : Float {
return sound.bytesLoaded;
}
override private function updateVolume(){
// Set the volume, if the channel isn't null
if (channel != null){
var transform = channel.soundTransform;
transform.volume = volume;
channel.soundTransform = transform;
}
}
override function soundOpen(e : OpenEvent){
channel = e.channel;
super.soundOpen(e);
}
override function stop(){
trafficControlTimer.stop();
if(!playBuffer){
stopOldChannel(); // this goes here in order to stop an old buffer if the users presses STOP button.
}
if (channel != null){
if(!playBuffer){
channel.stop();
}else{
switchChannels();
}
channel = null;
super.stop();
}
ui.setStatus(PlayerStatus.stop);
playBuffer=false;
}
function stopOldChannel(){
if(oldPlayingChannel!=null){
oldPlayingChannel.stop();
oldPlayingChannel=null;
}
}
function playingOldChannel():Bool{
if(oldPlayingChannel==null){
return false;
}
if(oldPlayingPosition==oldPlayingChannel.position){
return false;
}
oldPlayingPosition=oldPlayingChannel.position;
return true;
}
function setOldChannel(){
stopOldChannel();
oldPlayingChannel=channel;
oldPlayingPosition=oldPlayingChannel.position;
}
function switchChannels(e=null){
if(e==null){
if(oldPlayingChannel!=null) return;
setOldChannel();
}else{
if(sound!=null && playing && !sound.isBuffering && sound.bytesLoaded > 0){
stopOldChannel();
return;
}
if(!playingOldChannel()){
stopOldChannel();
return;
}
}
switchChannelTimer.reset();
switchChannelTimer.start();
}
override function isBuffering(){
return sound!=null && sound.isBuffering;
}
override function forceSynchronization() {
var oc = channel;
channel = sound.play(oc.position+128);
oc.stop();
updateVolume();
}
}