Skip to content

Commit 9812d34

Browse files
committed
Fix audio not loading reliably in firefox.
1 parent 4770f2b commit 9812d34

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

lute/templates/read/index.html

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,35 @@ <h2>&#127881;</h2>
234234

235235
<script>
236236
$(document).ready(function () {
237-
goto_relative_page(0);
238-
initialize_player();
237+
show_player();
238+
goto_relative_page(0, true);
239239
});
240240

241-
// If book_audio_file is not null, stream the file.
242-
let initialize_player = function() {
241+
/**
242+
* If book_audio_file is not null, set up the player.
243+
*
244+
* Don't actually load the source -- for some reason,
245+
* the ajax page load was causing the audio src to
246+
* only load occasionally! Not sure why. Source loading
247+
* is handled in load_player_source().
248+
*/
249+
let show_player = function() {
243250
const have_file = ($('#book_audio_file').val() != '');
244-
if (!have_file) {
245-
return;
251+
if (have_file) {
252+
$('div.audio-player-container').show();
246253
}
254+
}
247255

248-
$('div.audio-player-container').show();
249-
t = parseFloat($('#book_audio_current_pos').val());
256+
/**
257+
* Actually set the player source, loading the file,
258+
* and load all bookmarks etc.
259+
*/
260+
let load_player_source = function() {
250261
const book_id = $('#book_id').val();
262+
// console.log('setting source to ' + `/useraudio/stream/${book_id}`);
263+
player.src = `/useraudio/stream/${book_id}`;
264+
265+
t = parseFloat($('#book_audio_current_pos').val());
251266
player.currentTime = t;
252267
timeline.value = t;
253268

@@ -260,7 +275,6 @@ <h2>&#127881;</h2>
260275
bookmarksArray.push(b);
261276
}
262277

263-
player.src = `/useraudio/stream/${book_id}`;
264278
start_player_post_loop();
265279
};
266280

@@ -320,8 +334,16 @@ <h2>&#127881;</h2>
320334
};
321335

322336

323-
// Go to page relative to that stored in page_num input.
324-
function goto_relative_page(p) {
337+
/**
338+
* Go to page relative to that stored in page_num input.
339+
*
340+
* On first load of page, load_source_file is set to true.
341+
* It appears that for Firefox the player source can't be set
342+
* while the page content is being ajax'd in. This makes
343+
* _NO SENSE AT ALL_ but moving the player source loading
344+
* to the ajax "success" function worked. ???
345+
*/
346+
function goto_relative_page(p, load_source_file = false) {
325347
const bookid = $('#book_id').val();
326348
const pagenum = parseInt($('#page_num').val());
327349
const maxpage = parseInt($('#page_count').val());
@@ -344,6 +366,12 @@ <h2>&#127881;</h2>
344366
set_page_fraction();
345367
set_footer_control_visibility();
346368
show_hide_title(relpage);
369+
370+
// Magic hack for player source to load consistently
371+
// in Firefox.
372+
if (load_source_file) {
373+
load_player_source();
374+
}
347375
}
348376
);
349377
}

0 commit comments

Comments
 (0)