我正在开发一个音乐应用程序,它应该在后台播放音乐。
我用MPMoviePlayerController
播放音乐。我的代码来启动MPMoviePlayerController
:
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/music.m4a"];
NSError* err;
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:resourcePath]];
if (err) {
NSLog(@"ERROR: %@", err.localizedDescription);
}
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[session setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self.player setShouldAutoplay:NO];
[self.player setControlStyle: MPMovieControlStyleEmbedded];
self.player.view.hidden = YES;
[self.player prepareToPlay];
当我执行时[self.player play];
音乐开始。
但我还想在锁屏和控制中心显示歌曲名称、专辑名称和专辑插图。我正在使用以下代码:
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:@"artwork.png"]];
[songInfo setObject:@"SongName" forKey:MPMediaItemPropertyTitle];
[songInfo setObject:@"ArtistName" forKey:MPMediaItemPropertyArtist];
[songInfo setObject:@"AlbumTitle" forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
但锁屏中没有显示任何内容。它也不会显示在 ControlCenter 中。
我该如何解决我的问题?我在互联网上没有找到任何东西。
预先感谢,法比安。
问题是您不满足成为锁屏和控制中心大师的要求,因为我在书中解释 http://www.apeth.com/iOSBook/ch27.html#_remote_control_of_your_sound。您应该看到与此等效的现代(iOS 8):
您没有看到它的事实表明您忽略了一个或多个要求,我列出了这些要求(直接引用我的书中的内容):
- 您的应用程序的响应者链中必须包含一个 UIResponder
返回 YES 从
canBecomeFirstResponder
,并且该响应者必须
实际上是第一响应者。
- 响应者链中的一些UIResponder,
等于或高于第一响应者,必须实施
remoteControlReceivedWithEvent:
.
- 您的应用程序必须调用 UIApplication
实例方法
beginReceivingRemoteControlEvents
.
- 您的应用程序的音频
会话的策略必须是播放。
- 您的应用程序必须发出一些声音。
我不知道你忽略了哪一个;它可能不止一个。您可能想将您的代码与工作示例进行比较,如下所示:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch14p653backgroundPlayerAndInterrupter/backgroundPlayer/backgroundPlayer/ViewController.m https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch14p653backgroundPlayerAndInterrupter/backgroundPlayer/backgroundPlayer/ViewController.m
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)