質問編集履歴

2

訂正しました

2021/08/26 05:40

投稿

mocchirin
mocchirin

スコア2

test CHANGED
File without changes
test CHANGED
@@ -1,10 +1,14 @@
1
1
  ElectronでWebアプリを開発していて、音声を鳴らすときに0Bしか読み込まれません。
2
2
 
3
+ ![イメージ説明](d943d031c3cb384a6738c2d65318f74b.png)
4
+
3
- ら良いしょうか。
5
+ このボタンを押すと再生が始まるよに作っすが、押すと
4
6
 
5
7
  ![イメージ説明](67fc7a7a0425e4603a7c0e5a9b3fe944.png)
6
8
 
7
- ![メージ説明](998c73b1e7931801d58d74ed95e7002c.png)
9
+ のようになぜか0Bのファルが読み込まれます。
10
+
11
+ どうしたら再生されますか?
8
12
 
9
13
  ```javascript
10
14
 

1

コード追加

2021/08/26 05:39

投稿

mocchirin
mocchirin

スコア2

test CHANGED
File without changes
test CHANGED
@@ -5,3 +5,473 @@
5
5
  ![イメージ説明](67fc7a7a0425e4603a7c0e5a9b3fe944.png)
6
6
 
7
7
  ![イメージ説明](998c73b1e7931801d58d74ed95e7002c.png)
8
+
9
+ ```javascript
10
+
11
+ $(function () {
12
+
13
+ var playerTrack = $("#player-track"),
14
+
15
+ bgArtwork = $('#bg-artwork'),
16
+
17
+ bgArtworkUrl, albumName = $('#album-name'),
18
+
19
+ trackName = $('#track-name'),
20
+
21
+ albumArt = $('#album-art'),
22
+
23
+ sArea = $('#s-area'),
24
+
25
+ seekBar = $('#seek-bar'),
26
+
27
+ trackTime = $('#track-time'),
28
+
29
+ insTime = $('#ins-time'),
30
+
31
+ sHover = $('#s-hover'),
32
+
33
+ playPauseButton = $("#play-pause-button"),
34
+
35
+ i = playPauseButton.find('i'),
36
+
37
+ tProgress = $('#current-time'),
38
+
39
+ tTime = $('#track-length'),
40
+
41
+ seekT, seekLoc, seekBarPos, cM, ctMinutes, ctSeconds, curMinutes, curSeconds, durMinutes, durSeconds, playProgress, bTime, nTime = 0,
42
+
43
+ buffInterval = null,
44
+
45
+ tFlag = false,
46
+
47
+ albums = ['Holder', 'SiIvaGunner'],
48
+
49
+ trackNames = ['MINECRAFT THEME (TRAP REMIX)'],
50
+
51
+ albumArtworks = ['_1', '_2'],
52
+
53
+ trackUrl = [path.join(__dirname, 'assets', 'sounds', 'MINECRAFT THEME (TRAP REMIX) - Holder.webm'), path.join(__dirname, 'assets', 'sounds', 'Gourmet Race (64) (1.0.0) - Super Smash Bros. UItimate.webm')], playPreviousTrackButton = $('#play-previous'), playNextTrackButton = $('#play-next'), currIndex = -1;
54
+
55
+
56
+
57
+ function playPause() {
58
+
59
+ setTimeout(function () {
60
+
61
+ if (audio.paused) {
62
+
63
+ playerTrack.addClass('active');
64
+
65
+ albumArt.addClass('active');
66
+
67
+ checkBuffering();
68
+
69
+ i.attr('class', 'fas fa-pause');
70
+
71
+ audio.play();
72
+
73
+ }
74
+
75
+ else {
76
+
77
+ playerTrack.removeClass('active');
78
+
79
+ albumArt.removeClass('active');
80
+
81
+ clearInterval(buffInterval);
82
+
83
+ albumArt.removeClass('buffering');
84
+
85
+ i.attr('class', 'fas fa-play');
86
+
87
+ audio.pause();
88
+
89
+ }
90
+
91
+ }, 300);
92
+
93
+ }
94
+
95
+
96
+
97
+
98
+
99
+ function showHover(event) {
100
+
101
+ seekBarPos = sArea.offset();
102
+
103
+ seekT = event.clientX - seekBarPos.left;
104
+
105
+ seekLoc = audio.duration * (seekT / sArea.outerWidth());
106
+
107
+
108
+
109
+ sHover.width(seekT);
110
+
111
+
112
+
113
+ cM = seekLoc / 60;
114
+
115
+
116
+
117
+ ctMinutes = Math.floor(cM);
118
+
119
+ ctSeconds = Math.floor(seekLoc - ctMinutes * 60);
120
+
121
+
122
+
123
+ if ((ctMinutes < 0) || (ctSeconds < 0))
124
+
125
+ return;
126
+
127
+
128
+
129
+ if ((ctMinutes < 0) || (ctSeconds < 0))
130
+
131
+ return;
132
+
133
+
134
+
135
+ if (ctMinutes < 10)
136
+
137
+ ctMinutes = '0' + ctMinutes;
138
+
139
+ if (ctSeconds < 10)
140
+
141
+ ctSeconds = '0' + ctSeconds;
142
+
143
+
144
+
145
+ if (isNaN(ctMinutes) || isNaN(ctSeconds))
146
+
147
+ insTime.text('--:--');
148
+
149
+ else
150
+
151
+ insTime.text(ctMinutes + ':' + ctSeconds);
152
+
153
+
154
+
155
+ insTime.css({ 'left': seekT, 'margin-left': '-21px' }).fadeIn(0);
156
+
157
+
158
+
159
+ }
160
+
161
+
162
+
163
+ function hideHover() {
164
+
165
+ sHover.width(0);
166
+
167
+ insTime.text('00:00').css({ 'left': '0px', 'margin-left': '0px' }).fadeOut(0);
168
+
169
+ }
170
+
171
+
172
+
173
+ function playFromClickedPos() {
174
+
175
+ audio.currentTime = seekLoc;
176
+
177
+ seekBar.width(seekT);
178
+
179
+ hideHover();
180
+
181
+ }
182
+
183
+
184
+
185
+ function updateCurrTime() {
186
+
187
+ nTime = new Date();
188
+
189
+ nTime = nTime.getTime();
190
+
191
+
192
+
193
+ if (!tFlag) {
194
+
195
+ tFlag = true;
196
+
197
+ trackTime.addClass('active');
198
+
199
+ }
200
+
201
+
202
+
203
+ curMinutes = Math.floor(audio.currentTime / 60);
204
+
205
+ curSeconds = Math.floor(audio.currentTime - curMinutes * 60);
206
+
207
+
208
+
209
+ durMinutes = Math.floor(audio.duration / 60);
210
+
211
+ durSeconds = Math.floor(audio.duration - durMinutes * 60);
212
+
213
+
214
+
215
+ playProgress = (audio.currentTime / audio.duration) * 100;
216
+
217
+
218
+
219
+ if (curMinutes < 10)
220
+
221
+ curMinutes = '0' + curMinutes;
222
+
223
+ if (curSeconds < 10)
224
+
225
+ curSeconds = '0' + curSeconds;
226
+
227
+
228
+
229
+ if (durMinutes < 10)
230
+
231
+ durMinutes = '0' + durMinutes;
232
+
233
+ if (durSeconds < 10)
234
+
235
+ durSeconds = '0' + durSeconds;
236
+
237
+
238
+
239
+ if (isNaN(curMinutes) || isNaN(curSeconds))
240
+
241
+ tProgress.text('00:00');
242
+
243
+ else
244
+
245
+ tProgress.text(curMinutes + ':' + curSeconds);
246
+
247
+
248
+
249
+ if (isNaN(durMinutes) || isNaN(durSeconds))
250
+
251
+ tTime.text('00:00');
252
+
253
+ else
254
+
255
+ tTime.text(durMinutes + ':' + durSeconds);
256
+
257
+
258
+
259
+ if (isNaN(curMinutes) || isNaN(curSeconds) || isNaN(durMinutes) || isNaN(durSeconds))
260
+
261
+ trackTime.removeClass('active');
262
+
263
+ else
264
+
265
+ trackTime.addClass('active');
266
+
267
+
268
+
269
+
270
+
271
+ seekBar.width(playProgress + '%');
272
+
273
+
274
+
275
+ if (playProgress == 100) {
276
+
277
+ i.attr('class', 'fa fa-play');
278
+
279
+ seekBar.width(0);
280
+
281
+ tProgress.text('00:00');
282
+
283
+ albumArt.removeClass('buffering').removeClass('active');
284
+
285
+ clearInterval(buffInterval);
286
+
287
+ }
288
+
289
+ }
290
+
291
+
292
+
293
+ function checkBuffering() {
294
+
295
+ clearInterval(buffInterval);
296
+
297
+ buffInterval = setInterval(function () {
298
+
299
+ if ((nTime == 0) || (bTime - nTime) > 1000)
300
+
301
+ albumArt.addClass('buffering');
302
+
303
+ else
304
+
305
+ albumArt.removeClass('buffering');
306
+
307
+
308
+
309
+ bTime = new Date();
310
+
311
+ bTime = bTime.getTime();
312
+
313
+
314
+
315
+ }, 100);
316
+
317
+ }
318
+
319
+
320
+
321
+ function selectTrack(flag) {
322
+
323
+ if (flag == 0 || flag == 1)
324
+
325
+ ++currIndex;
326
+
327
+ else
328
+
329
+ --currIndex;
330
+
331
+
332
+
333
+ if ((currIndex > -1) && (currIndex < albumArtworks.length)) {
334
+
335
+ if (flag == 0)
336
+
337
+ i.attr('class', 'fa fa-play');
338
+
339
+ else {
340
+
341
+ albumArt.removeClass('buffering');
342
+
343
+ i.attr('class', 'fa fa-pause');
344
+
345
+ }
346
+
347
+
348
+
349
+ seekBar.width(0);
350
+
351
+ trackTime.removeClass('active');
352
+
353
+ tProgress.text('00:00');
354
+
355
+ tTime.text('00:00');
356
+
357
+
358
+
359
+ currAlbum = albums[currIndex];
360
+
361
+ currTrackName = trackNames[currIndex];
362
+
363
+ currArtwork = albumArtworks[currIndex];
364
+
365
+
366
+
367
+ audio.src = trackUrl[currIndex];
368
+
369
+
370
+
371
+ nTime = 0;
372
+
373
+ bTime = new Date();
374
+
375
+ bTime = bTime.getTime();
376
+
377
+
378
+
379
+ if (flag != 0) {
380
+
381
+ audio.play();
382
+
383
+ playerTrack.addClass('active');
384
+
385
+ albumArt.addClass('active');
386
+
387
+
388
+
389
+ clearInterval(buffInterval);
390
+
391
+ checkBuffering();
392
+
393
+ }
394
+
395
+
396
+
397
+ albumName.text(currAlbum);
398
+
399
+ trackName.text(currTrackName);
400
+
401
+ albumArt.find('img.active').removeClass('active');
402
+
403
+ $('#' + currArtwork).addClass('active');
404
+
405
+
406
+
407
+ bgArtworkUrl = $('#' + currArtwork).attr('src');
408
+
409
+
410
+
411
+ bgArtwork.css({ 'background-image': 'url(' + bgArtworkUrl + ')' });
412
+
413
+ }
414
+
415
+ else {
416
+
417
+ if (flag == 0 || flag == 1)
418
+
419
+ --currIndex;
420
+
421
+ else
422
+
423
+ ++currIndex;
424
+
425
+ }
426
+
427
+ }
428
+
429
+
430
+
431
+ function initPlayer() {
432
+
433
+ audio = new Audio();
434
+
435
+
436
+
437
+ selectTrack(0);
438
+
439
+
440
+
441
+ audio.loop = false;
442
+
443
+
444
+
445
+ playPauseButton.on('click', playPause);
446
+
447
+
448
+
449
+ sArea.mousemove(function (event) { showHover(event); });
450
+
451
+
452
+
453
+ sArea.mouseout(hideHover);
454
+
455
+
456
+
457
+ sArea.on('click', playFromClickedPos);
458
+
459
+
460
+
461
+ $(audio).on('timeupdate', updateCurrTime);
462
+
463
+
464
+
465
+ playPreviousTrackButton.on('click', function () { selectTrack(-1); });
466
+
467
+ playNextTrackButton.on('click', function () { selectTrack(1); });
468
+
469
+ }
470
+
471
+
472
+
473
+ initPlayer();
474
+
475
+ });
476
+
477
+ ```