2013-03-11 51 views
3

我目前有一個AVQueuePlayer按順序播放幾個.aif文件。當它通過每個音頻時,我想執行與該特定聲音相關的操作(例如,更改imageview的uiimage)。檢測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

有人嗎?我真的很感激它。 – KingPolygon 2013-03-11 21:44:57

回答

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

普山的回答是缺少循環觸發通知每個項目,所以:

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