|
36 | 36 | persist(); |
37 | 37 | } |
38 | 38 |
|
39 | | - /* indicator only – used for Mixcloud / YouTube embeds */ |
| 39 | + /* indicator only – used for Mixcloud / YouTube embeds. |
| 40 | + If local audio is currently playing we leave the player bar alone; |
| 41 | + the embed is an iframe we can't control anyway. */ |
40 | 42 | function setNowPlaying(data) { |
| 43 | + if (audio && !audio.paused) return; |
41 | 44 | killAudio(); |
42 | 45 | state = { |
43 | 46 | id: data.id, |
|
56 | 59 | var s = JSON.parse(localStorage.getItem(KEY)); |
57 | 60 | if (!s || s.type !== 'local_audio' || (!s.src && !s.audio_url)) return; |
58 | 61 | state = s; |
| 62 | + state.playing = false; |
59 | 63 | renderFull(); |
| 64 | + syncBtn(); |
60 | 65 | audio = new Audio(state.audio_url || state.src); |
61 | 66 | audio.addEventListener('timeupdate', tick); |
62 | 67 | audio.addEventListener('ended', onEnded); |
63 | | - /* browsers block autoplay without gesture – show paused state */ |
64 | | - audio.currentTime = state.time || 0; |
65 | | - state.playing = false; |
66 | | - syncBtn(); |
| 68 | + /* seek only after the browser knows the duration; earlier calls are silently dropped */ |
| 69 | + audio.addEventListener('loadedmetadata', function onMeta() { |
| 70 | + audio.removeEventListener('loadedmetadata', onMeta); |
| 71 | + audio.currentTime = state.time || 0; |
| 72 | + tick(); // paint progress bar at the saved position |
| 73 | + }); |
| 74 | + /* pulsing hint so user knows audio was playing */ |
| 75 | + var mid = document.getElementById('kp-mid'); |
| 76 | + if (mid) { |
| 77 | + var hint = document.createElement('div'); |
| 78 | + hint.id = 'kp-resume'; |
| 79 | + hint.className = 'kp-resume'; |
| 80 | + hint.textContent = '\u25B6 TAP TO RESUME'; |
| 81 | + mid.insertBefore(hint, mid.firstChild); |
| 82 | + } |
67 | 83 | } catch (e) { /* ignore */ } |
68 | 84 | } |
69 | 85 |
|
70 | 86 | function togglePlay() { |
71 | 87 | if (!audio) return; |
| 88 | + var hint = document.getElementById('kp-resume'); |
| 89 | + if (hint) hint.parentNode.removeChild(hint); |
72 | 90 | if (audio.paused) { audio.play(); state.playing = true; } |
73 | 91 | else { audio.pause(); state.playing = false; } |
74 | 92 | syncBtn(); |
|
0 commit comments