gpt4 book ai didi

ios - remoteControlReceivedWithEvent 在 iOS 7.0 设备上调用,但在 iOS 8.0 上不调用

转载 作者:可可西里 更新时间:2023-11-01 04:18:16 25 4
gpt4 key购买 nike

我有一个在后台播放音频的应用程序。我正在尝试修复主屏幕(等)上的音频控件(播放/暂停)在 iOS 8.0+ 上不起作用但在 iOS 7.0 上运行良好的错误。我一直在努力找出问题所在,但一无所获。任何想法将不胜感激。这是我所拥有的。

在项目设置中:我已确保将 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;

最佳答案

终于解决了我遇到的问题。最终,主屏幕上远程控制的事件似乎从未进入我的应用程序,也没有进入我的 View Controller 。我最终将 UIWindow 子类化,这样我就可以看到哪些事件正在通过链。由于 UIWindow 是一个 UIResponder,我还向子类添加了 - (void)remoteControlReceivedWithEvent:(UIEvent *)event。然后在 makeKeyAndVisible 中我添加了:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

我启动了调试器,但从未调用过 -(void)makeKeyAndVisible!然后,我在我的应用程序委托(delegate)中搜索了 window 成员变量,但找不到 [window makeKeyAndVisible]; 行!我把它加回去了(因为它本来应该在那里的),并且 presto 事件像魔术一样路由到正确的位置。为什么这适用于某些版本的 iOS 而不是其他版本并且没有任何其他明显的问题超出了我的范围。

希望这对以后的人有所帮助。

关于ios - remoteControlReceivedWithEvent 在 iOS 7.0 设备上调用,但在 iOS 8.0 上不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com