@@ -245,6 +245,7 @@ MainView {
245245 artist: "",
246246 image: "",
247247 guid: "",
248+ position: 0,
248249 }
249250
250251 source = source.toString()
@@ -255,26 +256,50 @@ MainView {
255256 function toggle() {
256257 if (playbackState === MediaPlayer.PlayingState) {
257258 pause()
259+ // Save the current position when we pause
260+ savePosition()
258261 } else {
259262 play()
263+ // Clear the last saved position when we start playing again
264+ clearPosition()
260265 }
261266 }
262267
263− function playEpisode(guid, image, name, artist, url) {
268+ function savePosition() {
269+ if (currentGuid) {
270+ var db = Podcasts.init()
271+ db.transaction(function (tx) {
272+ tx.executeSql("UPDATE Episode SET position=? WHERE guid=?", [player.position, currentGuid])
273+ tx.executeSql("UPDATE Queue SET position=? WHERE guid=?", [player.position, currentGuid])
274+ })
275+ }
276+ }
277+
278+ function clearPosition() {
279+ if (currentGuid) {
280+ var db = Podcasts.init()
281+ db.transaction(function (tx) {
282+ tx.executeSql("UPDATE Episode SET position=NULL WHERE guid=?", [currentGuid])
283+ })
284+ }
285+ }
286+
287+ function playEpisode(guid, image, name, artist, url, position) {
264288 // Clear current queue
265289 player.playlist.clear()
266290 Podcasts.clearQueue()
267291
268292 // Add episode to queue
269− Podcasts.addItemToQueue(guid, image, name, artist, url)
293+ Podcasts.addItemToQueue(guid, image, name, artist, url, position)
270294 player.playlist.addItem(url)
271−
295+
272296 // Play episode
297+ pendingSeek = position
273298 player.play()
274299 }
275300
276− function addEpisodeToQueue(guid, image, name, artist, url) {
277− Podcasts.addItemToQueue(guid, image, name, artist, url)
301+ function addEpisodeToQueue(guid, image, name, artist, url, position) {
302+ Podcasts.addItemToQueue(guid, image, name, artist, url, position)
278303 player.playlist.addItem(url)
279304
280305 // If added episode is the first one in the queue, then set the current metadata
@@ -286,11 +311,13 @@ MainView {
286311 currentArtist = artist
287312 currentImage = image
288313 currentUrl = url
314+ pendingSeek = position
289315 }
290316 }
291317
292318 property bool endOfMedia: false
293319 property double progress: 0
320+ property int pendingSeek: 0
294321
295322 playlist: Playlist {
296323 playbackMode: Playlist.Sequential
@@ -305,11 +332,28 @@ MainView {
305332 currentArtist = meta.artist
306333 currentImage = meta.image
307334 currentGuid = meta.guid
335+ player.pendingSeek = meta.position
336+ }
337+ }
338+
339+ onPlaybackStateChanged: {
340+ if (playbackState === MediaPlayer.PlayingState && pendingSeek !== 0) {
341+ player.seek(pendingSeek)
342+ player.clearPosition()
343+ pendingSeek = 0;
308344 }
309345 }
310346
311347 onStatusChanged: {
312− if (status === MediaPlayer.EndOfMedia) {
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) {
313357 console.log("[LOG]: End of Media. Stopping.")
314358 endOfMedia = true
315359 stop()
@@ -325,14 +369,17 @@ MainView {
325369 // Play then pause otherwise when we come from EndOfMedia
326370 // it calls next() until EndOfMedia again.
327371 play()
372+ pause()
328373 }
329−
330− pause()
331374 }
332375
333376 // Always reset endOfMedia
334377 endOfMedia = false
335378 }
379+
380+ Component.onDestruction: {
381+ savePosition()
382+ }
336383 }
337384
338385 PageStack {