MPMusicPlayerController Does Not Remember Playback Position - ios

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

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];

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.

Related

can I have library items my app plays show up as recently played in the Music app?

My iOS app plays music from the user's library via MPMusicPlayerController.applicationMusicPlayer. It can either be from Apple Music or manually synced music files.
Is there a way to have that music show up as recently played in the Music app?
Turns out yes, it happens naturally if you launch playback of an album, as in querying the album by a MPMediaEntityPersistentID and giving the resulting MPMediaItemCollection as a queue to the player.
What I was doing though is creating a custom song queue for playback, usually from a given album, and then I'd expect the Music app to show that album as recently played. But the Music app won't pick it up that way.

MPMediaItem and AVPlayerItem playback sequence in background

I'm having an issue with playing sequences of different kinds of items in a background.
In an app I'm working on we've introduced playlists which contain both content provided by the app and Apple Music content.
For that use AVPlayer and MPMusicPlayerController respectively. We observe one player or the other (depending what content is now playing) and if the other kind of content comes next, we release the old player (if we can - MPMusicPlayerController is a singleton, so best we can do is stop it) and load item to another player.
The problem starts when the app leaves foreground. Once MPMusicPlayerController takes over it doesn't want to give up control, so if any AVPlayer content comes after MPMusicPlayerController content, the music stops.
One workaround that I've tried is playing with .mixWithOthers options when I set the category on AVAudioSession, however this creates new category of problems - I'm loosing lockscreen controls, therefore I'm also loosing airplay. One dirty trick that I've tried was setting .mixWithOthers 3 seconds before MPMediaItem ends, and then disabling it back once AVPlayer starts. Beside the fact that there're probably many different things that can go wrong here, MPMediaPlayerController still doesn't want to give me back the control over lockscreen controls.
Is there any way this could ever work on iOS 13?

Do i need to stop MusicPlayerController first before i play to next song in iOS?

In my app , i have to play songs from iPod Library with MPMusicPlayerContrller's iPodMusicPlayer.
I just want to know , in my app , first song is playing from Album and when i want to play another song from Playlists that selected from UITableView do i need to use stop function [self.player stop]; before change next songs?
Or i can directly play without stop current playing song?
Thanks for your help.
no need to stop.my question is "Why to stop?". no,never nothing, there is no need of stop.
ios already given you facility of playing new song from playlist through the properties.
if you stops it & again play it,its just wastage of execution time.
Since you probably use skipToNextItem or skipToPreviousItem it's perfectly legit to use them directly.
Documentation for MPMusicPlayerController.

Full-featured music player using AVPlayer

I'm writing a music player for iOS that needs to have all the features of the built-in Music app. My app needs to continue running in the background so I have to use the AVPlayer class.
Are there any open source implementations out there that I can use instead of writing the whole thing myself?
Just found this. It works great:
https://github.com/gangverk/GVMusicPlayerController
If you want to play tracks from your iTunes music library, and don't want to use the MPMusicPlayerController class, your best bet is to use AVPlayer or AVQueuePlayer (subclass of AVPlayer). You must establish the appropriate audio session and register to receive remote control events for the app to continue playing music in the background.
There are downsides to this method; you won't be able to play DRM-protected tracks and audiobooks purchased from the iTunes store. There's no way to instantiate an iTunes Match download with the AVPlayer class. Furthermore, you'll have a bit of work on your hands if you want to add gapless playback and equaliser settings (The closest you'll get to gapless playback is with the AVQueuePlayer subclass, though in theory, you could overlap AVPlayers with an NSTimer).
You'll also need to change 'Required Background Modes' in your Info.plist to 'App plays audio'
As for the rest of your app, I suggest you read up on UITabBarControllers and UITableViewControllers along with MPMediaQuerys.
See this solution for the audio part.

IOS - Accessing Audio Stream from Ipod, Pausing and Changing Volume

I am working on an app that needs to pause and unpause the audio stream coming from the ipod app. Is there any way to do that? I want to make a button that once you press it, it pauses the currently playing song on the ipod app. Pressing it again unpauses where the music left off. I am also working on a way to change the volume of the ipod app, is there anyway to do this that does not rely on the user manually moving a slider?
Any help at all would be great. Thanks!
you could use this framework if you want: Media Player Framework
Specially the MPMusicPlayerController Class
You have this:
Controlling Playback
– play
– pause
– stop
Managing Playback Mode and State
volume ( property )

Resources