Brunwo commited on
Commit
c4395d2
1 Parent(s): 48414d2

improved pwa audio controls ?

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. script.js +76 -31
.gitignore CHANGED
@@ -8,3 +8,4 @@ flagged/log.csv
8
  locales/fr/LC_MESSAGES/messages.mo
9
  locales/en/LC_MESSAGES/messages.mo
10
  node_modules/
 
 
8
  locales/fr/LC_MESSAGES/messages.mo
9
  locales/en/LC_MESSAGES/messages.mo
10
  node_modules/
11
+ dist/
script.js CHANGED
@@ -4,6 +4,17 @@ import { Client } from "@gradio/client";
4
  let audioCache = {};
5
  let currentTrack = null;
6
 
 
 
 
 
 
 
 
 
 
 
 
7
  document.addEventListener("DOMContentLoaded", async function() {
8
  const audioPlayer = document.getElementById('player');
9
  const playButton = document.getElementById('playButton');
@@ -197,6 +208,9 @@ document.addEventListener("DOMContentLoaded", async function() {
197
 
198
  await loadAudioFromCache(link);
199
 
 
 
 
200
  } catch (error) {
201
  console.error('Error in fetchMp3:', error);
202
  console.error('Error stack:', error.stack);
@@ -245,6 +259,9 @@ document.addEventListener("DOMContentLoaded", async function() {
245
  }
246
 
247
  console.log('Audio loaded from cache and ready for playback');
 
 
 
248
  }
249
 
250
  async function saveAudioCache(link, audioUrl) {
@@ -319,6 +336,9 @@ document.addEventListener("DOMContentLoaded", async function() {
319
  }
320
  }, 5000);
321
 
 
 
 
322
  // Get the link from the shared URL
323
  const queryParams = new URLSearchParams(window.location.search);
324
  const sharedLink = queryParams.get('url');
@@ -333,34 +353,59 @@ document.addEventListener("DOMContentLoaded", async function() {
333
  console.log("No URL provided. Waiting for user input.");
334
  // You might want to update the UI here to indicate that the user needs to provide a URL
335
  }
336
- });
337
-
338
- if ('mediaSession' in navigator) {
339
- navigator.mediaSession.metadata = new MediaMetadata({
340
- title: 'Sample MP3',
341
- artist: 'Unknown Artist',
342
- album: 'Demo Album',
343
- artwork: [
344
- { src: '/icons/icon-192x192.png', sizes: '192x192', type: 'image/png' },
345
- { src: '/icons/icon-512x512.png', sizes: '512x512', type: 'image/png' }
346
- ]
347
- });
348
-
349
- navigator.mediaSession.setActionHandler('play', function() {
350
- audioPlayer.play();
351
- playButton.textContent = 'Pause';
352
- });
353
-
354
- navigator.mediaSession.setActionHandler('pause', function() {
355
- audioPlayer.pause();
356
- playButton.textContent = 'Play';
357
- });
358
-
359
- navigator.mediaSession.setActionHandler('seekbackward', function() {
360
- audioPlayer.currentTime = Math.max(audioPlayer.currentTime - 10, 0);
361
- });
362
-
363
- navigator.mediaSession.setActionHandler('seekforward', function() {
364
- audioPlayer.currentTime = Math.min(audioPlayer.currentTime + 10, audioPlayer.duration);
365
- });
366
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  let audioCache = {};
5
  let currentTrack = null;
6
 
7
+ // // Add this function at the beginning of your script.js file
8
+ // function handleSharedUrl() {
9
+ // const urlParams = new URLSearchParams(window.location.search);
10
+ // const sharedUrl = urlParams.get('url');
11
+
12
+ // if (sharedUrl) {
13
+ // console.log('Shared URL detected:', sharedUrl);
14
+ // fetchMp3(sharedUrl);
15
+ // }
16
+ // }
17
+
18
  document.addEventListener("DOMContentLoaded", async function() {
19
  const audioPlayer = document.getElementById('player');
20
  const playButton = document.getElementById('playButton');
 
208
 
209
  await loadAudioFromCache(link);
210
 
211
+ // Update media session metadata
212
+ updateMediaSessionMetadata(link, 'Web to Audio', 'Generated Audio');
213
+
214
  } catch (error) {
215
  console.error('Error in fetchMp3:', error);
216
  console.error('Error stack:', error.stack);
 
259
  }
260
 
261
  console.log('Audio loaded from cache and ready for playback');
262
+
263
+ // Update media session metadata
264
+ updateMediaSessionMetadata(link, 'Web to Audio', 'Generated Audio');
265
  }
266
 
267
  async function saveAudioCache(link, audioUrl) {
 
336
  }
337
  }, 5000);
338
 
339
+ // Call handleSharedUrl instead of directly checking for the URL parameter
340
+ // handleSharedUrl();
341
+
342
  // Get the link from the shared URL
343
  const queryParams = new URLSearchParams(window.location.search);
344
  const sharedLink = queryParams.get('url');
 
353
  console.log("No URL provided. Waiting for user input.");
354
  // You might want to update the UI here to indicate that the user needs to provide a URL
355
  }
356
+
357
+ // Add this function to update media session metadata
358
+ function updateMediaSessionMetadata(title, artist, album) {
359
+ if ('mediaSession' in navigator) {
360
+ navigator.mediaSession.metadata = new MediaMetadata({
361
+ title: title || 'Unknown Title',
362
+ artist: artist || 'Unknown Artist',
363
+ album: album || 'Unknown Album',
364
+ artwork: [
365
+ { src: '/icons/imagepodcast-transp500.png', sizes: '500x500', type: 'image/png' },
366
+ { src: '/icons/imagepodcast.png', sizes: '1024x1024', type: 'image/png' }
367
+ ]
368
+ });
369
+ }
370
+ }
371
+
372
+ // Add this function to set up media session handlers
373
+ function setupMediaSessionHandlers() {
374
+ if ('mediaSession' in navigator) {
375
+ navigator.mediaSession.setActionHandler('play', () => {
376
+ audioPlayer.play();
377
+ playButton.textContent = 'Pause';
378
+ });
379
+
380
+ navigator.mediaSession.setActionHandler('pause', () => {
381
+ audioPlayer.pause();
382
+ playButton.textContent = 'Play';
383
+ });
384
+
385
+ navigator.mediaSession.setActionHandler('seekbackward', (details) => {
386
+ const skipTime = details.seekOffset || 10;
387
+ audioPlayer.currentTime = Math.max(audioPlayer.currentTime - skipTime, 0);
388
+ });
389
+
390
+ navigator.mediaSession.setActionHandler('seekforward', (details) => {
391
+ const skipTime = details.seekOffset || 10;
392
+ audioPlayer.currentTime = Math.min(audioPlayer.currentTime + skipTime, audioPlayer.duration);
393
+ });
394
+
395
+ navigator.mediaSession.setActionHandler('seekto', (details) => {
396
+ if (details.fastSeek && 'fastSeek' in audioPlayer) {
397
+ audioPlayer.fastSeek(details.seekTime);
398
+ return;
399
+ }
400
+ audioPlayer.currentTime = details.seekTime;
401
+ });
402
+
403
+ navigator.mediaSession.setActionHandler('previoustrack', () => {
404
+ audioPlayer.currentTime = 0;
405
+ });
406
+ }
407
+ }
408
+
409
+ // Call this function to set up the media session handlers
410
+ setupMediaSessionHandlers();
411
+ });