我有一个在后台播放音频的应用程序。我正在尝试修复主屏幕(等)上音频控件(播放/暂停)的错误,不能在iOS 8.0上运行,但在iOS 7.0上运行FINE。我一直在努力弄清楚问题是什么,并且一直空洞。任何想法将不胜感激。这就是我所拥有的。
在项目设置中:
我确保将UIBackgroundModes
设置为audio
。
在AppDelegate.h中:
我有AVAudioSession* session;
以及AVPlayer *audioPlayer;
在AppDelegate.m中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.session = [AVAudioSession sharedInstance];
NSError* error = NULL;
[self.session setCategory:AVAudioSessionCategoryPlayback error:&error];
[self.session setActive:YES error:&error];
if (error) {
NSLog(@"AVAudioSession error: %@", [error localizedDescription]);
}
在AudioPlayerViewController.m
中- (void)viewDidLoad {
//Get the Audio
NSURL *url = [NSURL URLWithString:self.audioUrl];
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
//Setup the player
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
appDelegate.audioPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
//Setup the "Now Playing"
NSMutableDictionary *mediaInfo = [[NSMutableDictionary alloc]init];
[mediaInfo setObject:self.title forKey:MPMediaItemPropertyTitle];
[mediaInfo setObject:self.artist forKey:MPMediaItemPropertyArtist];
[mediaInfo setObject:self.album forKey:MPMediaItemPropertyAlbumTitle];
[mediaInfo setObject:[NSNumber numberWithDouble:duration ] forKey:MPMediaItemPropertyPlaybackDuration];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mediaInfo];
}
// Process remote control events
- (void) remoteControlReceivedWithEvent:(UIEvent *)event {
NSLog(@"AudioPlayerViewController ... remoteControlReceivedWithEvent top ....subtype: %d", event.subtype);
if (event.type == UIEventTypeRemoteControl) {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self togglePlayPause];
break;
case UIEventSubtypeRemoteControlPause:
[self doPause];
break;
case UIEventSubtypeRemoteControlStop:
[self doPause];
break;
case UIEventSubtypeRemoteControlPlay:
[self doPlay];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self nextOrPrevTrack];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextOrPrevTrack];
break;
default:
break;
}
}
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
答案 0 :(得分:8)
终于找到了我遇到的问题。最终似乎主屏幕上远程控制的事件从未进入我的应用程序,直到我的视图控制器。我最终继承了UIWindow
,因此我可以看到哪些事件正在通过链条。由于UIWindow
是UIResponder
,我还将- (void)remoteControlReceivedWithEvent:(UIEvent *)event
添加到子类中。然后在makeKeyAndVisible中添加了:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
我启动了调试器,-(void)makeKeyAndVisible
从未被调用过!然后我在我的应用代表中搜索了window
成员变量,[window makeKeyAndVisible];
行无处可寻!我把它添加回来(因为它应该在那里)并且presto事件正在路由到像魔法一样的正确位置。为什么这适用于某些版本的iOS而不是其他版本,没有任何其他明显的问题超出了我的范围。
希望这可以帮助将来的某个人。
答案 1 :(得分:4)
在ViewController中添加
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
self.becomeFirstResponder()
}
override func remoteControlReceivedWithEvent(event: UIEvent) {
// your stuff
}
在AppDelegate中添加
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var error: NSError?
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &error)
AVAudioSession.sharedInstance().setActive(true, error: &error)
}
SWIFT 3
UIApplication.shared.beginReceivingRemoteControlEvents()
self.becomeFirstResponder()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("hmmm...")
}