Change lock screen background audio controls text?
Asked Answered
O

2

27

I have an iOS app that streams background audio using AVAudioSession. It is working correctly, but I am curious, is there any way to change the text on the lock screen audio controls? Right now it simply displays the name of my app, but I would like to change it to the name of the track.

Additionally, the multi-tasking bar has no text under the controls- is there a way to add the track name there, as the iPod app does?

Overexpose answered 6/7, 2011 at 17:33 Comment(1)
Anyone know how to do it with web audio?Sluiter
E
33

iOS 5 now supports setting the track title as well as an album art image in both the lock screen and the remote playback controls (the controls you get when you double-click the home button and swipe right). Take a look at the MPNowPlayingInfoCenter class. Of course, to maximize compatibility, you'd want to test whether MPNowPlayingInfoCenter is available, by doing something like:

if ([MPNowPlayingInfoCenter class])  {
   /* we're on iOS 5, so set up the now playing center */
   UIImage *albumArtImage = [UIImage imageNamed:@"HitchHikersGuide"];
   albumArt = [[MPMediaItemArtwork alloc] initWithImage:albumArtImage];

    NSDictionary *currentlyPlayingTrackInfo = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Life, the Universe and Everything", [NSNumber numberWithInt:42], albumArt, nil] forKeys:[NSArray arrayWithObjects:MPMediaItemPropertyTitle, MPMediaItemPropertyPlaybackDuration, MPMediaItemPropertyArtwork, nil]];
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
}
Essonite answered 17/10, 2011 at 18:6 Comment(2)
You want to use classFromString here as this will cause lower os's to crashInoue
Nope. No official way of setting this in iOS 4.x. I don't even think there's an unofficial way of doing this.Essonite
S
4

here it is in swift! (no need to check for iOS 5 and up anymore)

let albumArt = MPMediaItemArtwork(image: UIImage(named:"HitchHikersGuide"))
let albumDict = [MPMediaItemPropertyTitle: "Life, the Universe and Everything", MPMediaItemPropertyPlaybackDuration: 42, MPMediaItemPropertyArtwork: albumArt]
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = albumDict
Solvable answered 3/9, 2015 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.