-
Notifications
You must be signed in to change notification settings - Fork 0
/
KAudioTool.js
190 lines (148 loc) · 7.09 KB
/
KAudioTool.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
(function(kLib) {
var ids = 0;
function generateId()
{
ids++;
var id = "KAudioTool"+ids;
return id;
}
//kLib.activeCourriers = kLib.activeCourriers || {};
function KAudioToolOptions(rawOptions)
{
var public = this;
rawOptions = rawOptions || {};
public.id = rawOptions.id || generateId();
public.audioTag = rawOptions.audioTag || document.createElement("audio");
public.playToggleButtonClassName = rawOptions.playToggleButtonClassName || "k-audio-play-toggle-button";
public.playAtLaunch = rawOptions.playAtLaunch || [false];
public.playToggleButtonTag = rawOptions.playToggleButtonTag || [];//document.createElement("div");
public.nextButtonClassName = rawOptions.nextButtonClassName || "k-audio-next-button";
public.nextButtonTag = rawOptions.nextButtonTag || [];// document.createElement("div");
public.wrapperTag = rawOptions.wrapperTag || [];// document.createElement("div");
public.audioItemClassName = rawOptions.audioItemClassName || "k-podcast-list-item";
}
function initKAudioTool(rawOptions)
{
var validOptions = new KAudioToolOptions(rawOptions);
var result = new KAudioTool(validOptions);
kLib.activeAudioTools = kLib.activeAudioTools || {};
kLib.activeAudioTools[result.id] = result;
return result;
}
KAudioToolOptions.validate = function(options)
{
if(!options) throw "Missing KAudioTool options";
var testAgent = new KAudioToolOptions({id:0});
kLib.iterateObject(testAgent,function(key,value){
if(!options[key]) throw "KAudioTool options missing: "+ key;
});
return options;
}
function KAudioTool(kAudioToolOptions)
{
var options = KAudioToolOptions.validate(kAudioToolOptions);
var public = this;
var defaults = {};
var audioTag = options.audioTag;
var givens = {};
defaults.onProgression = function(proressionRatio) {kLib.log("playing audio the progression is:"+proressionRatio);}
defaults.onPlayCallBack = function() { kLib.log("play button clicked on "); }
defaults.onNextCallBack = function(){kLib.log("play button clicked on "); }
defaults.onPlayIndexCallBack = function(feedItem){
kLib.log("clicked on activating ");
kLib.log(feedItem);
}
kLib.iterateObject(defaults,function(key,value){
givens[key] = value;
});
public.feedItems = [];
public.currentIndex = 0;
public.options = options;
public.id = options.id;
public.audioTag = audioTag;
public.activate = activate;
public.playToggleButtonElement = options.playToggleButtonTag;
public.audioItemClassName = options.audioItemClassName;
public.wrapperTag = options.wrapperTag;
public.nextButtonElement = options.nextButtonTag;
public.innerPauseHTML = "pause";
public.innerPlayHTML = "play";
public.setOnProgression = function(fn) { givens.onProgression = fn.bind(this);}
public.setOnPlayClicked = function(fn){ givens.onPlayCallBack = fn.bind(this); }
public.setPlayIndexCallBack = function(fn){ givens.onPlayIndexCallBack = fn.bind(this); }
public.setOnNextClicked = function(fn){ givens.onNextCallBack = fn.bind(this); }
public.playAtLaunch = options.playAtLaunch[0];
function validateFeedItem(feedItem)
{
if(!feedItem) throw "no feed item provided";
if(!feedItem.mp3Link) throw "audio feed needs mp3Link attribute";
return feedItem;
}
function playNext()
{
public.currentIndex ++;
var index = public.currentIndex;
var feedItem = public.feedItems[index];
feedItem = validateFeedItem(feedItem);
audioTag.setAttribute("src",feedItem.mp3Link);
audioTag.play();
public.playToggleButtonElement.innerHTML = public.innerPauseHTML ;
givens.onNextCallBack(feedItem);
}
function playIndex(index)
{
var feedItem = public.feedItems[index];
feedItem = validateFeedItem(feedItem);
audioTag.setAttribute("src",feedItem.mp3Link);
audioTag.play();
public.playToggleButtonElement.innerHTML = public.innerPauseHTML ;
givens.onPlayIndexCallBack(feedItem);
}
function activate(indexToStartWith)
{
/** public.feedItems = [];
public.currentIndex = 0; */
var index = indexToStartWith || public.currentIndex;
var feedItem = public.feedItems[index];
feedItem = validateFeedItem(feedItem);
audioTag.setAttribute("src",feedItem.mp3Link);
public.audioTag.setAttribute("src",feedItem.mp3Link);
public.nextButtonElement = kLib.isElement(public.nextButtonElement) ? public.nextButtonElement: kLib.getFirstChildWithClassName(public.wrapperTag,options.nextButtonClassName);
public.nextButtonElement.style.cursor = "pointer";
public.nextButtonElement.onclick = playNext.bind(this);
public.playToggleButtonElement = kLib.isElement(public.playToggleButtonElement) ?public.playToggleButtonElement: kLib.getFirstChildWithClassName(public.wrapperTag,options.playToggleButtonClassName);
public.playToggleButtonElement.style.cursor = "pointer";
public.playToggleButtonElement.onclick =(function()
{
var itWasPaused = audioTag.paused;
var thingTodo = itWasPaused ? audioTag.play.bind(audioTag) :audioTag.pause.bind(audioTag);
public.playToggleButtonElement.innerHTML = itWasPaused ? public.innerPauseHTML : public.innerPlayHTML ;
thingTodo();
givens.onPlayCallBack();
}).bind(this);
kLib.doThisToElementsWithClassName(public.audioItemClassName,function(element)
{
var index = kLib.getData(element,"index");
element.onclick = function(){playIndex(index);};
//playIndex(index);
//kLib.log(index);
});
audioTag.ontimeupdate = function()
{
var progressionRatio = (audioTag.currentTime/audioTag.duration);
givens.onProgression(progressionRatio);
if(progressionRatio<1 || !audioTag.paused ) return;
public.playToggleButtonElement.innerHTML = public.innerPlayHTML;
}
if(audioTag.paused && public.playAtLaunch )
{
audioTag.play();
kLib.log("-------------------------------------------------->here");
kLib.log(public.innerPauseHTML);
kLib.log(public.playToggleButtonElement);
public.playToggleButtonElement.innerHTML = public.innerPauseHTML ;
}
}
}
kLib.initAudioTool = initKAudioTool;
})(kLib)