2013-03-11 5 views
3

В настоящее время у меня есть AVQueuePlayer, играющий несколько файлов .aif в последовательности. По мере прохождения каждого звука я хочу выполнить действие, связанное с этим конкретным звуком (например, изменение образа изображения).Обнаружение текущего элемента в AVQueuePlayer и знание, когда оно изменяется?

Моя проблема заключается в правильной настройке NSNotification. Я установил его, чтобы уведомить меня, когда очередь закончилась с последним объектом, но у меня возникли проблемы с получением уведомления для каждого текущего элемента. Вот что у меня есть:

//Setting Up My Queue List 
    yellowVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/YellowVoice.aif", [[NSBundle mainBundle] resourcePath]]]]; 
    orangeVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/OrangeVoice.aif", [[NSBundle mainBundle] resourcePath]]]]; 
    redVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/RedVoice.aif", [[NSBundle mainBundle] resourcePath]]]]; 
    pinkVoice = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/PinkVoice.aif", [[NSBundle mainBundle] resourcePath]]]]; 

    NSArray *soundEmotions = [NSArray arrayWithObjects:yellowVoice, orangeVoice, redVoice, pinkVoice, nil]; 

    soundQueue = [AVQueuePlayer queuePlayerWithItems:soundEmotions]; 

// Notify me when queue reaches the end of the last player item 
[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(playerItemDidReachEnd:) 
               name:AVPlayerItemDidPlayToEndTimeNotification 
               object:[soundEmotions lastObject]]; 

// Notify me when a new player item begins and which one it is 
[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(currentItemIs:) 
               name:AVPlayerItemDidPlayToEndTimeNotification 
               object:[soundQueue currentItem]]; 

[soundQueue play]; 

Мне кажется, что я застрял с параметром имени. Что я должен изменить? И как я могу узнать имя игрока, который сейчас играет, когда он переключается? Благодаря! Ответ

//Here is one of the things I want to do with that info 
- (void)currentItemIs:(NSNotification *)notification { 

    if (soundQueue.currentItem ==yellowVoice) { 

     UIImage * yellowImage = [UIImage imageNamed:@"yellow.png"]; 
     [UIView transitionWithView:self.view 
          duration:1.0f 
          options:UIViewAnimationOptionTransitionCrossDissolve 
         animations:^{ 
          mainImage.image = yellowImage; 
         } completion:NULL]; 


    } 


    if (soundQueue.currentItem ==yellowVoice) { 

     UIImage * orangeImage = [UIImage imageNamed:@"orange.png"]; 
     [UIView transitionWithView:self.view 
          duration:1.0f 
          options:UIViewAnimationOptionTransitionCrossDissolve 
         animations:^{ 
          mainImage.image = orangeImage; 
         } completion:NULL]; 


    } 


} 
+0

Anyone? Я бы очень признателен. – KingPolygon

ответ

3
- (void)currentItemIs:(NSNotification *)notification 
{ 
    AVPlayerItem *p = [notification object]; 
    if (p ==yellowVoice) { 
    //rest of the code 
    } 
} 
2

BHUSHAN является отсутствие петли для запуска уведомления для каждого элемента, так:

for (AVPlayerItem *playerItem in queuePlayer.items) { 
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentItemIs:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; 
     }