Skip to content

Commit a809d18

Browse files
committed
update simple example
1 parent a584d97 commit a809d18

File tree

4 files changed

+69
-75
lines changed

4 files changed

+69
-75
lines changed

dist/library/core.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ export declare class PlayerCore {
2828
readonly WHERE_IN_QUEUE_AT_END: string;
2929
readonly WHERE_IN_QUEUE_AT_START: string;
3030
readonly WHERE_IN_QUEUE_AFTER_CURRENT: string;
31-
readonly PLAY_SOUND_NEXT = "next";
32-
readonly PLAY_SOUND_PREVIOUS = "previous";
33-
readonly PLAY_SOUND_FIRST = "first";
34-
readonly PLAY_SOUND_LAST = "last";
31+
readonly PLAY_SOUND_NEXT = 'next';
32+
readonly PLAY_SOUND_PREVIOUS = 'previous';
33+
readonly PLAY_SOUND_FIRST = 'first';
34+
readonly PLAY_SOUND_LAST = 'last';
3535
constructor(playerOptions?: ICoreOptions);
3636
protected _initialize(): void;
3737
addSoundToQueue(soundAttributes: ISoundAttributes, whereInQueue?: string): ISound;

examples/simple-player/client/bootstrap.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
// player build
5-
import { PlayerCore, PlayerSound, ICoreOptions } from '../../../dist/index.js';
5+
import { PlayerCore, ISoundAttributes, ICoreOptions } from '../../../dist/index.js';
66

77
// library
88
import { PlayerUI } from './library/player/ui.js';
@@ -15,24 +15,17 @@ let options: ICoreOptions = {
1515
let player = new PlayerCore(options);
1616
let playerUI = new PlayerUI(player);
1717

18-
19-
20-
/*
21-
22-
23-
24-
2518
let firstSoundAttributes: ISoundAttributes = {
2619
sources: '1314412&format=mp31',
2720
id: 1314412,
2821
playlistId: 0,
2922
onLoading: (loadingProgress, maximumValue, currentValue) => {
3023
console.log('loading: ', loadingProgress, maximumValue, currentValue);
31-
playerUI.setLoadingProgress(loadingProgress);
24+
playerUI.changeLoadingProgress(loadingProgress);
3225
},
3326
onPlaying: (playingProgress, maximumValue, currentValue) => {
3427
console.log('playing: ', playingProgress, maximumValue, currentValue);
35-
playerUI.setPlayingProgress(playingProgress);
28+
playerUI.changePlayingProgress(playingProgress);
3629
},
3730
onStarted: (playTimeOffset) => {
3831
console.log('started', playTimeOffset);
@@ -49,7 +42,7 @@ let firstSoundAttributes: ISoundAttributes = {
4942
onEnded: (willPlayNext) => {
5043
console.log('ended', willPlayNext);
5144
if (!willPlayNext) {
52-
playerUI.switchPlayerContext('on');
45+
playerUI.resetUI();
5346
}
5447
}
5548
};
@@ -63,11 +56,11 @@ let secondSoundAttributes: ISoundAttributes = {
6356
playlistId: 0,
6457
onLoading: (loadingProgress, maximumValue, currentValue) => {
6558
console.log('loading: ', loadingProgress, maximumValue, currentValue);
66-
playerUI.setLoadingProgress(loadingProgress);
59+
playerUI.changeLoadingProgress(loadingProgress);
6760
},
6861
onPlaying: (playingProgress, maximumValue, currentValue) => {
6962
console.log('playing: ', playingProgress, maximumValue, currentValue);
70-
playerUI.setPlayingProgress(playingProgress);
63+
playerUI.changePlayingProgress(playingProgress);
7164
},
7265
onStarted: (playTimeOffset) => {
7366
console.log('started', playTimeOffset);
@@ -84,14 +77,14 @@ let secondSoundAttributes: ISoundAttributes = {
8477
onEnded: (willPlayNext) => {
8578
console.log('ended', willPlayNext);
8679
if (!willPlayNext) {
87-
playerUI.switchPlayerContext('on');
80+
playerUI.resetUI();
8881
}
8982
}
9083
};
9184

9285
// add another song
9386
let secondSound = player.addSoundToQueue(secondSoundAttributes);
94-
*/
87+
9588

9689

9790

examples/simple-player/client/library/player/ui.ts

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,21 @@ class PlayerUI {
77
public player: PlayerCore;
88

99
protected _buttonsBox: HTMLElement;
10+
protected _volumeSlider: HTMLInputElement;
11+
protected _loadingProgressBar: HTMLInputElement;
12+
protected _playingProgressBar: HTMLInputElement;
1013

1114
constructor(player: PlayerCore) {
1215

1316
this.player = player;
1417

15-
this.prepareUI();
16-
17-
}
18-
19-
private prepareUI() {
20-
21-
// buttons box
22-
this._buttonsBox = document.getElementById('js-buttons-box');
18+
this._prepareUI();
2319

2420
}
2521

26-
}
27-
28-
/*
29-
30-
protected _buttonsBox: HTMLElement;
31-
protected _volumeSlider: HTMLInputElement;
32-
protected _loadingProgressBar: HTMLInputElement;
33-
protected _playingProgressBar: HTMLInputElement;
34-
35-
constructor(player: PlayerCore) {
36-
37-
// compatibility goal: minimum IE11
38-
22+
protected _prepareUI() {
3923

24+
// NOTE TO SELF: compatibility goal: minimum IE11
4025

4126
// buttons box
4227
this._buttonsBox = document.getElementById('js-buttons-box');
@@ -53,8 +38,9 @@ class PlayerUI {
5338
// start listening to events
5439
this._createListeners();
5540

56-
// set the initial volume volume to the volume input range
41+
// set the initial volume to the volume input range
5742
this._volumeSlider.value = String(this.player.getVolume());
43+
// TODO: use the localstorage or indexeddb to persist the user volume
5844

5945
}
6046

@@ -94,20 +80,18 @@ class PlayerUI {
9480
break;
9581
}
9682

97-
this.switchPlayerContext(playerContext);
83+
this._switchPlayerContext(playerContext);
9884

9985
}
10086

10187
if ($button.id === 'js-previous-button') {
10288

103-
this.setPlayingProgress(0);
89+
this._setPlayingProgress(0);
10490

10591
let playerContext = this._buttonsBox.dataset['playerContext'];
10692

10793
if (playerContext === 'off') {
108-
109-
this.switchPlayerContext(playerContext);
110-
94+
this._switchPlayerContext(playerContext);
11195
}
11296

11397
this.player.play('previous');
@@ -116,14 +100,12 @@ class PlayerUI {
116100

117101
if ($button.id === 'js-next-button') {
118102

119-
this.setPlayingProgress(0);
103+
this._setPlayingProgress(0);
120104

121105
let playerContext = this._buttonsBox.dataset['playerContext'];
122106

123107
if (playerContext === 'off') {
124-
125-
this.switchPlayerContext(playerContext);
126-
108+
this._switchPlayerContext(playerContext);
127109
}
128110

129111
this.player.play('next');
@@ -144,7 +126,28 @@ class PlayerUI {
144126

145127
}
146128

147-
public switchPlayerContext(currentPlayerContext: string) {
129+
protected _onChangeVolume(event: Event) {
130+
131+
// styling the html5 range:
132+
// http://brennaobrien.com/blog/2014/05/style-input-type-range-in-every-browser.html
133+
134+
let rangeElement = event.target as HTMLInputElement;
135+
let value = parseInt(rangeElement.value);
136+
137+
this.player.setVolume(value);
138+
139+
}
140+
141+
protected _onChangePlayingProgress(event: Event) {
142+
143+
let rangeElement = event.target as HTMLInputElement;
144+
let value = parseInt(rangeElement.value);
145+
146+
this.player.setPosition(value);
147+
148+
}
149+
150+
protected _switchPlayerContext(currentPlayerContext: string) {
148151

149152
let $playIcon = document.getElementById('js-play');
150153
let $pauseIcon = document.getElementById('js-pause');
@@ -169,24 +172,33 @@ class PlayerUI {
169172

170173
}
171174

172-
protected _onChangeVolume(event: Event) {
175+
protected _setPlayingProgress(percentage: number) {
173176

174-
// styling the html5 range:
175-
// http://brennaobrien.com/blog/2014/05/style-input-type-range-in-every-browser.html
177+
this._playingProgressBar.value = percentage.toString();
176178

177-
let rangeElement = event.target as HTMLInputElement;
178-
let value = parseInt(rangeElement.value);
179+
}
179180

180-
this.player.setVolume(value);
181+
protected _setLoadingProgress(percentage: number) {
182+
183+
this._loadingProgressBar.value = percentage.toString();
181184

182185
}
183186

184-
protected _onChangePlayingProgress(event: Event) {
187+
public changePlayingProgress(percentage: number) {
185188

186-
let rangeElement = event.target as HTMLInputElement;
187-
let value = parseInt(rangeElement.value);
189+
this._setPlayingProgress(percentage);
188190

189-
this.player.setPosition(value);
191+
}
192+
193+
public changeLoadingProgress(percentage: number) {
194+
195+
this._setLoadingProgress(percentage);
196+
197+
}
198+
199+
public resetUI() {
200+
201+
this._switchPlayerContext('on');
190202

191203
}
192204

@@ -202,24 +214,12 @@ class PlayerUI {
202214

203215
}
204216

205-
public setLoadingProgress(percentage: number) {
206-
207-
this._loadingProgressBar.value = percentage.toString();
208-
209-
}
210-
211-
public setPlayingProgress(percentage: number) {
212-
213-
this._playingProgressBar.value = percentage.toString();
214-
215-
}
216-
217217
public deconstructor() {
218218

219219
this._destroyListeners();
220220

221221
}
222-
*/
223222

223+
}
224224

225225
export { PlayerUI };

examples/simple-player/html/main.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ <h1>web audio api player - simple example</h1>
4444
<button id="js-repeat-button" class="button">R</button>
4545
</div>
4646

47-
<input type="range" min="0" max="100" value="0" step="1" id="js-player-loading-progress" />
47+
<progress max="100" value="0" id="js-player-loading-progress"></progress>
4848

49+
<!-- NOTE TO SELF: I use the range element instead of progress to allow the user to interact with it -->
4950
<input type="range" min="0" max="100" value="0" step="1" id="js-player-playing-progress" />
5051

5152
<input type="range" min="0" max="100" value="0" step="1" id="js-player-volume" />

0 commit comments

Comments
 (0)