- Home /
iOS MP3 Player with iOS Control Center / Background Music
I want to create a MP3 Player app for Android and iOS. I figured out, that when Unity is put in the background a sound from an AudioSoure stops playing (which isn't that much of a suprise).
Since I have experience with Android I think I can handle this: I will use a service to manage a MediaPlayer. To this Service I will pass "Playlists". These Playlists will be json arrays of MediaItems. A MediaItem will contain artist album path to artwork path to mp3
Artwork and MP3 will be stored in Application.persistendDatapath, for both, Android and iOS.
Using a service allows me to continue playing even if the app is in background. I'm also planing implementing lockscreen and notificationbar controll. Unity will be called if the user presses such a button. Also Unity will be able to get the current MediaItem.
The question is, how can I achive all this on iOS? I'm very very new to iOS and Objective-C (that language seams pretty ugly compared to languages I know o.O). I want to use the iOS Control Center / the IPod Dock (don't know what these things are actually called).
I'm gratefull for any code/tutorial/link/plugin etc related to this. I also hope that my plans are understandable.
Best regards Eric
Did you ever get this sorted? I'm looking to do something similar!
Answer by domharris · Mar 16, 2018 at 04:20 PM
@badben I managed to do it in the end, here's what I used! In Xcode, you also need to go to "Capabilities" and enable "Background Modes" and check "Audio, AirPlay and Picture in Picture". You'll need to do that every time you build from Unity.
NSMutableDictionary *nowPlayingInfo = [NSMutableDictionary dictionary];
[nowPlayingInfo setValue:@"Your app name" forKey:MPMediaItemPropertyTitle];
nowPlayingInfo[MPMediaItemPropertyTitle] = @"Your app name";
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @([self getPlayer].currentPlaybackTime);
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = @([self getPlayer].duration);
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlayingInfo];
self.pauseHandler = [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
NSLog(@"Pause it");
[player pause];
// call a function in Unity
UnitySendMessage("ObjectName", "FunctionName", "value to pass back");
NSMutableDictionary *nowPlayingInfo = [NSMutableDictionary dictionary];
[nowPlayingInfo setValue:@"Your app name" forKey:MPMediaItemPropertyTitle];
nowPlayingInfo[MPMediaItemPropertyTitle] = @"Your app name";
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @([self getPlayer].currentPlaybackTime);
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = @([self getPlayer].duration);
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlayingInfo];
NSError *error;
[[AVAudioSession sharedInstance] setActive:NO error:&error];
NSLog(@"ERROR: %@", error);
return MPRemoteCommandHandlerStatusSuccess; // or fail or no such content
}];
Sounds good but I haven't any idea where paste this code ... I don't know how to code in objective-c !
@awesome-alex I'd suggest looking up some Unity objective-c plugin tutorials!
@domharris you got a good tutorial suggestion ?
Your answer
Follow this Question
Related Questions
iOS Music Control 0 Answers
iOS game - Run GPS in background? 2 Answers
Run in background for iOS 3 Answers
Run a script in background when the app is open or closed 5 Answers