6

I'm making an app that will play audiobooks synced with iTunes. Is there a way my player can remember the playback position? Or do I need to implement this myself with some sort of database?

I'm testing on iOS 8.4

Hackmodford
  • 3,901
  • 4
  • 35
  • 78

2 Answers2

1

The key is to set the current playback time to the bookmark time of the mpmediaitem before playing.

Here's an example:

[self.player setQueueWithItemCollection:self.collection];
self.player.currentPlaybackTime = [self.collection.items[0] bookmarkTime]; 
[self.player play];
Hackmodford
  • 3,901
  • 4
  • 35
  • 78
0

An audiobook file will automatically remember its playback position, and when asked to play again later, will resume from that position - that is a built-in feature of the Music app (now iBooks) and therefore of the MPMusicPlayerController.

However, you will notice that the Music app can lose track of the currently playing item (if the user restarts the device). And of course the user might change the currently playing item manually.

Thus, if you want your app to return to what it was playing previously, you will have to save that currently playing item information yourself. And so you might as well save the current playback position too, thus making yourself even more reliable than the Music app.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I'm using [MPMusicPlayerController applicationMusicPlayer], but when I play an audiobook it always starts at the beginning. – Hackmodford Jul 11 '15 at 20:04
  • My answer in that case is even more right - you'll _definitely_ need to keep track of this yourself. – matt Jul 11 '15 at 22:15
  • I wonder how an app like ecoute handles this. It plays audiobook files and the iBooks app remembers to position. :/ – Hackmodford Jul 12 '15 at 00:38
  • That's why I suggested (first sentence of my answer) that this is a feature of the file itself. When I upload audiobook files thru iTunes to my iPhone, I make sure the Remember Position checkbox is checked for those files. – matt Jul 12 '15 at 00:51
  • I have to admit, though, that all my experience in this regard is with `[MPMusicPlayerController systemMusicPlayer]`, not `applicationMusicPlayer`. That's what my app uses. And thus it gets the same interplay with iBooks. – matt Jul 12 '15 at 01:18
  • I'm still having trouble. I create the systemMusicPlayer... I start playing the selected file. It always starts from the beginning. – Hackmodford Jul 12 '15 at 01:27
  • Is this with iOS 8.4 by any chance? – matt Jul 12 '15 at 01:54
  • Yes. And it's one of the reasons I'm making an app. – Hackmodford Jul 12 '15 at 02:45
  • I ask because 8.4, with its completely changed Music app, has broken my app. – matt Jul 12 '15 at 02:58
  • It's my fault I was having problems. I have given you the +50 reputation ;) – Hackmodford Jul 22 '15 at 22:38
  • It's very specific to my app. Suffice it to say I looked for all instances where I was setting the curentPlaybackTime and found one where it was setting it to zero after the view loaded ;) – Hackmodford Jul 23 '15 at 12:46
  • Now I'm confused, all of a sudden it's not playing at the correct position again. I've commented every line that changes the currentPlaybackTime :( – Hackmodford Jul 23 '15 at 17:48
  • Well, that's why I suggested this might be an iOS 8.4 issue. What I'm finding (and others have reported this too) is that the currently playing item / position are not correctly reported for audio books. – matt Jul 23 '15 at 18:10
  • I think I was actually wrong. It never was remembering the position :( It's weird because the app Ecoute is able to remember the position even if I change it using iBooks. I wonder if it's because they're using an older SDK? – Hackmodford Jul 23 '15 at 18:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84110/discussion-between-hackmodford-and-matt). – Hackmodford Jul 23 '15 at 18:44
  • "It's weird because the app Ecoute is able to remember the position even if I change it using iBooks" But that is why I said _you_ need to do the playing _internally_ and remember the position _internally_. That way, you don't _care_ what iBooks does. In other words, you use the `ApplicationMusicPlayer` and don't _try_ to interact with the Music app / iBooks — because, in iOS 8.4, I think you'll fail. – matt Jul 23 '15 at 19:12
  • I figured it out. The key is to get the bookmarktime of the mpmediaitem before you start playing it. [self.player setQueueWithItemCollection:self.collection]; self.player.currentPlaybackTime = [self.collection.items[0] bookmarkTime]; [self.player play]; Can you add this to your answer? – Hackmodford Jul 23 '15 at 21:02
  • I didn't know about the bookmark time. You can get it either as `bookmarkTime` or through `valueForProperty` with `MPMediaItemPropertyBookmarkTime`. Nevertheless this does not solve the bug I'm talking about, which is that the MPMusicPlayerController is failing to report its own `nowPlayingItem` and `currentPlaybackTime` correctly. – matt Jul 23 '15 at 23:52
  • It does solve my problem, and is extremely relevant to the original question ;) Could you please add it to your answer? – Hackmodford Jul 24 '15 at 00:17
  • I'm happy to, but I don't want to take credit for something I didn't know. You can answer your own question - and you can accept your answer instead of accepting mine! If you do, I'll also upvote your answer (to applaud you for solving it yourself). How does that sound? – matt Jul 24 '15 at 00:18
  • OK, I wanted to offer it to you anyways. – Hackmodford Jul 24 '15 at 00:36
  • That's great, but you already gave me the bounty out of the goodness of your heart. Now I want to see to it that you recover all that reputation! :) – matt Jul 24 '15 at 00:42