设置后台播放
设置App的plist,使app可以在后台播放音乐。
myApp-Info.plist中添加UIBackgroundModes键值,添加子键值为audio。
合适的位置添加代码
1 2 3
| AVAudioSession *session = [AVAudioSession sharedInstance]; [session setActive:YES error:nil]; [session setCategory:AVAudioSessionCategoryPlayback error:nil];
|
添加播放控制器(Remote Control Events)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; }
- (BOOL)canBecomeFirstResponder { return YES; }
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{ switch (event.subtype) { case UIEventSubtypeRemoteControlPlay: nslog(@"play"); break; case UIEventSubtypeRemoteControlPause: nslog(@"pause"); break; case UIEventSubtypeRemoteControlNextTrack: nslog(@"next"); break; case UIEventSubtypeRemoteControlPreviousTrack: nslog(@"previous");; break; default: break; } } UIEventSubtypeNone = 0, UIEventSubtypeMotionShake = 1, UIEventSubtypeRemoteControlPlay = 100, UIEventSubtypeRemoteControlPause = 101, UIEventSubtypeRemoteControlStop = 102, UIEventSubtypeRemoteControlTogglePlayPause = 103, UIEventSubtypeRemoteControlNextTrack = 104, UIEventSubtypeRemoteControlPreviousTrack = 105, UIEventSubtypeRemoteControlBeginSeekingBackward = 106, UIEventSubtypeRemoteControlEndSeekingBackward = 107, UIEventSubtypeRemoteControlBeginSeekingForward = 108, UIEventSubtypeRemoteControlEndSeekingForward = 109,
|
设置后台信息显示及锁屏界面设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #import <MediaPlayer/MediaPlayer.h> - (void)setLockingInfo { NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:@"题目" forKey:MPMediaItemPropertyTitle]; [dict setObject:@"歌手" forKey:MPMediaItemPropertyArtist]; [dict setObject:@"专辑" forKey:MPMediaItemPropertyAlbumTitle]; UIImage *newImage = [UIImage imageNamed:@"43.png"]; [dict setObject:[[MPMediaItemArtwork alloc] initWithImage:newImage] forKey:MPMediaItemPropertyArtwork]; [dict setObject:[NSNumber numberWithDouble:300] forKey:MPMediaItemPropertyPlaybackDuration]; [dict setObject:[NSNumber numberWithDouble:150] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict]; }
|
本人从事iOS开发行业多年,精通OC和Swift,会使用Flutter框架。