MPMusicPlayerController Does Not Remember Playback Position
Asked Answered
V

2

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

Verada answered 11/7, 2015 at 17:33 Comment(2)
Why does this question have so many upvotes? It shows no effort...Pedi
It's a good question ;)Verada
V
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];
Verada answered 24/7, 2015 at 0:38 Comment(0)
M
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.

Maihem answered 11/7, 2015 at 17:37 Comment(22)
I'm using [MPMusicPlayerController applicationMusicPlayer], but when I play an audiobook it always starts at the beginning.Verada
My answer in that case is even more right - you'll definitely need to keep track of this yourself.Maihem
I wonder how an app like ecoute handles this. It plays audiobook files and the iBooks app remembers to position. :/Verada
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.Maihem
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.Maihem
I'm still having trouble. I create the systemMusicPlayer... I start playing the selected file. It always starts from the beginning.Verada
Is this with iOS 8.4 by any chance?Maihem
Yes. And it's one of the reasons I'm making an app.Verada
I ask because 8.4, with its completely changed Music app, has broken my app.Maihem
It's my fault I was having problems. I have given you the +50 reputation ;)Verada
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 ;)Verada
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 :(Verada
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.Maihem
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?Verada
Let us continue this discussion in chat.Verada
"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.Maihem
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?Verada
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.Maihem
It does solve my problem, and is extremely relevant to the original question ;) Could you please add it to your answer?Verada
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?Maihem
OK, I wanted to offer it to you anyways.Verada
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! :)Maihem

© 2022 - 2024 — McMap. All rights reserved.