2

My app is divided into two modules. For first module, even after screen lock the audio should play and the locked screen should show details related to media content. However, in the second module no special requirement is there. The audio should be stopped as soon as the screen is locked and no controls should be visible on the locked screen.

For first module, I wrote the following piece of code

In my appdelegate I implemented:

- (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];
}

in MyVideoController class

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];
}

Upon screen lock I am seeing the controllers as expected. Everything is fine.

However, in my second module, I changed the audio session properties -

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

whenever a video is playing and I lock the screen I see the below image for 1 or 2 seconds and then it disappears! Probably, because the app suddenly realizes that the screen is locked and then decides to hide the controls. enter image description here

I believe, I shouldn't be seeing this screen in the first place. Is there any other way to disable the screen apart from what I have implemented?

EDIT 1:

Added the following piece in MyVideoController class

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

Again, seeing the screen for 1 second before it disappears.

A_G
  • 2,260
  • 3
  • 23
  • 56

0 Answers0