Подтвердить что ты не робот

Воспроизведение музыки на заднем фоне: AVAudioSessionInterruptionNotification не запускается

Мое приложение хорошо играет в . Пауза и play также хорошо работает, даже мое приложение является фоном, используя методы, зарегистрированные для AVAudioSessionInterruptionNotification.

Проблема приходит к сценарию:

  • Запустите мое приложение → идет фоновая музыка из моего приложения, играющего хорошо
  • запустите приложение и музыку для Apple. Ошибка AVAudioSessionInterruptionNotification.
  • Когда я приостанавливаю музыкальное приложение или убиваю музыкальное приложение. AVAudioSessionInterruptionNotification не запускается

код: Я делаю это в appdelegate. didfinishlaunching

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error: nil];

self.player = [[AVQueuePlayer alloc] initWithURL:[NSURL URLWithString:@"http://someurl.com:8040/;stream.mp3"]];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];




- (void) onAudioSessionEvent: (NSNotification *) notification
{
    //Check the type of notification, especially if you are sending multiple AVAudioSession events here
    NSLog(@"Interruption notification name %@", notification.name);

    if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
        NSLog(@"Interruption notification received %@!", notification);

        //Check to see if it was a Begin interruption
        if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
            NSLog(@"Interruption began!");

        } else if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){
            NSLog(@"Interruption ended!");
            //Resume your audio
            NSLog(@"Player status %i", self.player.status);
            // Resume playing the audio.
            [self.player play];

        }
    }
}
4b9b3361

Ответ 2

Существует свойство вашего экземпляра AVCaptureSession: @property (неатомный) BOOL используетApplicationAudioSession

Это значение YES по умолчанию просто устанавливает значение НЕТ и будет работать нормально.

Ответ 3

Попробуйте добавить объект: session

[[NSNotificationCenter defaultCenter] addObserver: self селектор: @selector (onAudioSessionEvent:) name: объект AVAudioSessionInterruptionNotification: [AVAudioSession sharedInstance]];