-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuzz.dart
136 lines (108 loc) · 3.18 KB
/
Buzz.dart
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
#library('Buzz');
#import('dart:html');
#import('dart:core');
#source('sound.dart');
/**
* [Buzz] class is used as a singelton object and contains helper method
* to wotk with [Sound] objects.
*/
class Buzz {
//
// Static Properties
//
static final defaults = const {
"autoplay" : false,
"duration" : 5000,
"formats" : const [],
"loop" : false,
"placeholder" : "--",
"preload": "metadata",
"volume": 80
};
static final types = const {
"mp3": "audio/mpeg",
"ogg": "audio/ogg",
"wav": "audio/wav",
"aac": "audio/aac",
"m4a": "audio/x-m4a"
};
static var sounds = null;
//
// Instance Properties
//
var _el;
//
// Singelton Implementation
//
static Buzz _singleRef;
static Buzz get Instance() => _singleRef != null ? _singleRef : new Buzz();
factory Buzz() {
if (_singleRef == null) {
_singleRef = new Buzz._internal();
}
return _singleRef;
}
Buzz._internal() {
sounds = new List<Sound>();
// Create the audio tag
_el = new Element.tag("audio");
// Add the new element to the document
document.body.nodes.add(_el);
}
//
// Public Methods
//
/*
* Check if the browser support for audio is present
*/
bool get isSupported() {
return (_el.canPlayType != null);
}
bool get isOGGSupported() {
return true;
//return isSupported && (_el.canPlayType('audio/ogg; codecs="vorbis"', null) == "probably");
}
bool get isWAVSupported() {
return true;
//return isSupported && (_el.canPlayType('audio/wav; codecs="1"','') == "probably");
}
bool get isMP3Supported() {
return true;
//return isSupported && (_el.canPlayType("audio/mpeg;",'') == "probably");
}
bool get isAACSupported() {
return true;
//return isSupported &&
// ( (_el.canPlayType( 'audio/x-m4a;','' ) == "probably") ||
// (_el.canPlayType( 'audio/aac;','' ) ) == "probably");
}
String toTimer(num time, bool withHours ) {
int h, m, s;
h = ( time / 3600 ).floor().toInt();
h = ( h ).isNaN() ? '--' : ( h >= 10 ) ? h : '0' + h;
m = withHours ? ( time / 60 % 60 ).floor().toInt() : ( time / 60 ).floor().toInt();
m = ( m ).isNaN() ? '--' : ( m >= 10 ) ? m : '0' + m;
s = ( time % 60 ).floor();
s = ( s ).isNaN() ? '--' : ( s >= 10 ) ? s : '0' + s;
return withHours ? "$h:$m:$s" : "$m:$s";
}
num fromTimer(String time) {
num retVal = 0;
var splits = time.split( ':' );
if ( splits != null && splits.length == 3 ) {
retVal = ( Math.parseInt( splits[ 0 ]) * 3600 ) + ( Math.parseInt(splits[ 1 ]) * 60 ) + Math.parseInt( splits[ 2 ]);
}
if ( splits != null && splits.length == 2 ) {
retVal = ( Math.parseInt(splits[ 0 ]) * 60 ) + Math.parseInt( splits[ 1 ]);
}
return retVal;
}
num toPercent( num value, num total, [num decimal = 0] ) {
var r = Math.pow( 10, decimal );
return ( ( ( value * 100 ) / total ) * r ).round() / r;
}
num fromPercent( num percent, num total, [num decimal = 0] ) {
var r = Math.pow( 10, decimal );
return ( ( ( total / 100 ) * percent ) * r ).round() / r;
}
}