Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zocker1996 · Aug 26, 2015 at 12:47 PM · iosbackgroundmusicnativebackground music

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image domharris · Mar 15, 2016 at 04:02 PM 0
Share

Did you ever get this sorted? I'm looking to do something similar!

avatar image BenoitFreslon · Mar 16, 2018 at 04:11 PM 0
Share

Hello Eric, did you found how to do this?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
 }];
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BenoitFreslon · Mar 16, 2018 at 06:19 PM 0
Share

Perfect! Thanks

avatar image DVFrance · Apr 24, 2018 at 02:19 PM 0
Share

Sounds good but I haven't any idea where paste this code ... I don't know how to code in objective-c !

avatar image domharris DVFrance · Apr 24, 2018 at 02:22 PM 0
Share

@awesome-alex I'd suggest looking up some Unity objective-c plugin tutorials!

avatar image DVFrance domharris · Apr 24, 2018 at 04:12 PM 0
Share

@domharris you got a good tutorial suggestion ?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

unity iOS bug - Duplicate native method found 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges