git.iko.soy/maciek134/podphoenix
on main help sign in
* Fix position saving and restoring on the episodes page

* Make metadata lookup more efficient * Refactor out duplicated code * Fix position restoration when switching between episodes on the same screen (i.e. without a model refresh) * Add workaround to reduce cases where media-hub gets confused and plays the wrong file

commit7cba1b3ed1daef647842fe70e01ebfcdc0e0eae6 authorMichael Sheldon <michael.sheldon@canonical.com> dateTue 3 Jan 2017, 00:02 (10 years ago) parent8ef9b3e
4 files changed+58−29
app/podbird.qml+28−15 app/podcasts.js+8−11 app/ui/EpisodesPage.qml+12−2 app/ui/EpisodesTab.qml+10−1
app/podbird.qml+28−15
@@ -221,6 +221,21 @@ MainView {
221221 }
222222 }
223223
224+ // This reduces incidences of media-hub getting confused and
225+ // continuing to play the previous file when clearing a playlist
226+ // and starting a new episode
227+ Timer {
228+ id: playStarter
229+ interval: 500
230+ onTriggered: {
231+ // Ideally we'd check the buffer progress here and base it on that
232+ // but media-hub doesn't report it correctly
233+ console.log("Starting playback")
234+ player.play()
235+ player.restorePosition()
236+ }
237+ }
238+
224239 MediaPlayer {
225240 id: player
226241
@@ -275,6 +290,14 @@ MainView {
275290 }
276291 }
277292
293+ function restorePosition() {
294+ if (playbackState === MediaPlayer.PlayingState && pendingSeek !== 0) {
295+ player.seek(pendingSeek)
296+ player.clearPosition()
297+ pendingSeek = 0;
298+ }
299+ }
300+
278301 function clearPosition() {
279302 if (currentGuid) {
280303 var db = Podcasts.init()
@@ -285,6 +308,8 @@ MainView {
285308 }
286309
287310 function playEpisode(guid, image, name, artist, url, position) {
311+ player.pause()
312+
288313 // Clear current queue
289314 player.playlist.clear()
290315 Podcasts.clearQueue()
@@ -295,7 +320,7 @@ MainView {
295320
296321 // Play episode
297322 pendingSeek = position
298− player.play()
323+ playStarter.restart()
299324 }
300325
301326 function addEpisodeToQueue(guid, image, name, artist, url, position) {
@@ -337,23 +362,11 @@ MainView {
337362 }
338363
339364 onPlaybackStateChanged: {
340− if (playbackState === MediaPlayer.PlayingState && pendingSeek !== 0) {
341− player.seek(pendingSeek)
342− player.clearPosition()
343− pendingSeek = 0;
344− }
365+ restorePosition()
345366 }
346367
347368 onStatusChanged: {
348− if (status === MediaPlayer.Loading) {
349− if (playbackState === MediaPlayer.PlayingState && pendingSeek !== 0) {
350− player.seek(pendingSeek)
351− player.clearPosition()
352− pendingSeek = 0;
353− } else {
354− play()
355− }
356− } else if (status === MediaPlayer.EndOfMedia) {
369+ if (status === MediaPlayer.EndOfMedia) {
357370 console.log("[LOG]: End of Media. Stopping.")
358371 endOfMedia = true
359372 stop()
app/podcasts.js+8−11
@@ -117,17 +117,14 @@ function lookup(source) {
117117 }
118118
119119 db.transaction(function(tx) {
120− var rs = tx.executeSql("SELECT * FROM Queue ORDER BY ind ASC");
121− for(var i = 0; i < rs.rows.length; i++) {
122− var episode = rs.rows.item(i);
123− if (source === episode.url) {
124− meta.name = episode.name
125− meta.artist = episode.artist
126− meta.image = episode.image
127− meta.guid = episode.guid
128− meta.position = episode.position
129− break
130− }
120+ var rs = tx.executeSql("SELECT * FROM Queue WHERE url = ?", [source]);
121+ if (rs.rows.length > 0) {
122+ var episode = rs.rows.item(0);
123+ meta.name = episode.name
124+ meta.artist = episode.artist
125+ meta.image = episode.image
126+ meta.guid = episode.guid
127+ meta.position = episode.position
131128 }
132129 });
133130
app/ui/EpisodesPage.qml+12−2
@@ -682,8 +682,18 @@ Page {
682682 selected = !selected
683683 } else {
684684 if (currentGuid !== model.guid) {
685− currentUrl = model.downloadedfile ? "file://" + model.downloadedfile : model.audiourl;
686− player.playEpisode(model.guid, model.image, model.name, model.artist, currentUrl)
685+ player.savePosition()
686+ currentUrl = model.downloadedfile ? "file://" + model.downloadedfile : model.audiourl
687+ // We need to refetch the episode position as it may have changed without the model refreshing
688+ var db = Podcasts.init()
689+ db.transaction(function (tx) {
690+ var position = 0
691+ var rs = tx.executeSql("SELECT position FROM Episode WHERE guid=? AND position > 0", [model.guid])
692+ if (rs.rows.length > 0) {
693+ position = rs.rows.item(0).position
694+ }
695+ player.playEpisode(model.guid, model.image, model.name, model.artist, currentUrl, position)
696+ });
687697 }
688698 }
689699 }
app/ui/EpisodesTab.qml+10−1
@@ -671,7 +671,16 @@ Tab {
671671 if (currentGuid !== model.guid) {
672672 player.savePosition()
673673 currentUrl = model.downloadedfile ? "file://" + model.downloadedfile : model.audiourl
674− player.playEpisode(model.guid, model.image, model.name, model.artist, currentUrl, model.position)
674+ // We need to refetch the episode position as it may have changed without the model refreshing
675+ var db = Podcasts.init()
676+ db.transaction(function (tx) {
677+ var position = 0
678+ var rs = tx.executeSql("SELECT position FROM Episode WHERE guid=? AND position > 0", [model.guid])
679+ if (rs.rows.length > 0) {
680+ position = rs.rows.item(0).position
681+ }
682+ player.playEpisode(model.guid, model.image, model.name, model.artist, currentUrl, position)
683+ });
675684 }
676685 }
677686 }