在运行时更改屏幕锁定控件

时间:2017-05-19 07:14:06

标签: ios objective-c audio screen-lock mpnowplayinginfocenter

我的应用分为两个模块。对于第一个模块,即使在屏幕锁定后,音频也应播放,锁定的屏幕应显示与媒体内容相关的详细信息。但是,在第二个模块中没有特殊要求。屏幕锁定后应立即停止音频,并且锁定屏幕上不应显示任何控件。

对于第一个模块,我编写了以下代码

在我的appdelegate中,我实施了:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter];
[[rcc skipForwardCommand] setEnabled:NO];
[[rcc skipBackwardCommand] setEnabled:NO];
[[rcc nextTrackCommand] setEnabled:NO];
[[rcc previousTrackCommand] setEnabled:NO];
[[rcc skipForwardCommand] setEnabled:NO];
[[rcc skipBackwardCommand] setEnabled:NO];
rcc.playCommand.enabled = YES;
rcc.pauseCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(play)];
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(pause)];
}

- (void) play {
[[MyVideoController instance] play];
}

- (void) pause {
[[MyVideoController instance] pause];
}

在MyVideoController类

AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
NSError *activationError = nil;
BOOL success = [[AVAudioSession sharedInstance] setActive: YES error: &activationError];

NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:@"Audio_Thumbnail_Play"]];
[songInfo setObject:title forKey:MPMediaItemPropertyTitle];
[songInfo setObject:@"duration" forKey:MPMediaItemPropertyPlaybackDuration];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
self.avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionModeDefault error: nil];
}

屏幕锁定后,我看到控制器正常。一切都很好。

但是,在我的第二个模块中,我更改了音频会话属性 -

-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionModeDefault error:nil];
[session setActive:NO error:nil];
}

每当视频正在播放并且我锁定屏幕时,我会看到下面的图像1或2秒然后它就消失了!可能是因为应用程序突然意识到屏幕被锁定然后决定隐藏控件。 enter image description here

我相信,我不应该首先看到这个屏幕。除了我实施的内容之外,还有其他方法可以禁用屏幕吗?

编辑1:

在MyVideoController类中添加了以下部分

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionModeDefault error: nil];
}

再次,在屏幕消失前看屏幕1秒钟。

0 个答案:

没有答案