4

I saw a note that this address http://developer.apple.com/library/ios/#qa/qa1668/_index.html

Note: If you disable the video tracks in the movie, or detach the AVPlayerLayer from its associated AVPlayer it will allow the movie audio to continue playing in the background. However, these changes must be in effect before the application is actually switched to the background.

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

   [[AVAudioSession sharedInstance] setDelegate: self];
   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
   [[AVAudioSession sharedInstance] setActive: YES error: nil];



    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:AnUrl]];
   AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];


   AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
   avPlayerLayer.frame = self.view.layer.bounds;
   UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
   [newView.layer addSublayer:avPlayerLayer];
   [self.view addSubview:newView];

   [avPlayer play];

This program works. But in the background, the sound is cut off. I want to continue to play the background only sound. How I detach between AVPlayerLayer from AVPlayer?

user1669335
  • 143
  • 10

1 Answers1

1

Your code above is okay, but do these things during transition to/from background:

  • When application becomes active: (Your avPlayerLayer must be the AVPlayerLayer you added to your view)

    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [avPlayerLayer playerLayerWithPlayer:avPlayer];
    
    }
    
  • When application goes into background:(Your avPlayerLayer must be the AVPlayerLayer you added to your view)

    - (void)applicationWillResignActive:(UIApplication *)application {
        [avPlayerLayer playerLayerWithPlayer:nil];
    }
    

Also, don't forget to add audio in UIBackgroundModes of your plist.

Cheers.

mltpyt
  • 96
  • 1
  • 8
  • I think this might be exactly what I need! Quick question (possibly noobish), though: `applicationWillResignActive` is an `AppDelegate` method, and all of my `AVPlayerLayer` code is in a separate ViewController file. What's the best way to be able to reference VideoViewController's player in the AppDelegate? Import VideoVideoController into AppDelegate, or is there an application-wide "get whatever AVPlayer is currently running" method or something? What's the best practice there? – Nerrolken Aug 14 '13 at 02:00
  • You can either use delegates, or notifications. (1) For the use of delegates, see this stackoverflow issue: http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c. (2) For notifications, please see this: http://stackoverflow.com/questions/9011868/whats-the-best-way-to-detect-when-the-app-is-entering-the-background-for-my-view. I would recommend using notifications. (use UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification). – mltpyt Aug 30 '13 at 01:20