Does iOS add sound played through AVPlayer to the default OS player?

So, the idea is that when we play audio in the Music application on the iPhone and press the home button and then scroll to the bottom of the screen, we can see the sound played in the OS player by default, with playback / pause controls and sound reproduction.

Now, what I need to do, I play audio using AVAudioPlayerin my application, and if the user presses the home button, the sound should play, as is the case with the music application. But I do not know how to add audio to the player by default OS? Any help or suggestion would be greatly appreciated.

Edit: I need to play audio using streaming, so I think I need to use AVPlayerinsteadAVAudioPlayer

+4
source share
2 answers

, "", . AVAudioSession. AVPlayerObjects AVQueuePlayer viewDidLoad. , http://www.raywenderlich.com/29948/backgrounding-for-ios. ( ), - (void) observValueForKeyPath.

( Ray Wenderlich):

in viewDidLoad:

// Set AVAudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance]     setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

// Change the default output audio route
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
  sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

NSArray *queue = @[
[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle]  URLForResource:@"IronBacon" withExtension:@"mp3"]],
[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"FeelinGood" withExtension:@"mp3"]],
[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"WhatYouWant" withExtension:@"mp3"]]];

self.player = [[AVQueuePlayer alloc] initWithItems:queue];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;

[self.player addObserver:self
              forKeyPath:@"currentItem"
                 options:NSKeyValueObservingOptionNew
                 context:nil];

:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"currentItem"])
    {
        AVPlayerItem *item = ((AVPlayer *)object).currentItem;
        self.lblMusicName.text = ((AVURLAsset*)item.asset).URL.pathComponents.lastObject;
        NSLog(@"New music name: %@", self.lblMusicName.text);
    }
}

- API- :

@interface TBFirstViewController ()

@property (nonatomic, strong) AVQueuePlayer *player;
@property (nonatomic, strong) id timeObserver; 
@end
+1

All Articles