0

我有一個隊列設置了四個曲目。當軌道改變時,我想改變一個UIImage,關於那個特定的軌道(如果軌道1正在播放,我想要顯示一個名爲1.png的圖像,如果軌道2正在播放,我想要顯示2.png等) 。在AVQueuePlayer中使用Switch語句?

我想使用switch語句,但我不確定如何使用它來設置表達式。

switch(soundEmotions AVPlayerItem) 
    { 
     case yellowVoice: 

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

      break; 
     case orangeVoice: 

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

      break; 
     case redVoice: 

      break; 
     case pinkVoice: 

      break; 
     default: 


      break; 
    } 

回答

2

switch語句需要一個整數。在這種情況下,您想要的整數是正在播放的當前AVPlayerItem的索引。

因此,請將AVPlayerItems數組傳遞到AVQueuePlayer的數組的副本。然後在這個數組中找到當前的玩家物品,並且你將得到你的索引值。

NSInteger index = [self.soundEmotions indexOfObject:self.player.currentItem]; 
NSString *imageName = nil; 
switch (index) { 
    case 0: 
     imageName = @"yellow"; // You don't need the ".png" part. 
     break: 
    case 1: 
     imageName = @"orange"; 
     break: 
    case 2: 
     imageName = @"red"; 
     break: 
    case 3: 
     imageName = @"pink"; 
     break: 
    default: 
     // Any other number or NSNotFound. 
     break: 
} 

if (imageName) { 
    [UIView transitionWithView:self.view 
         duration:1.0f 
         options:UIViewAnimationOptionTransitionCrossDissolve 
        animations:^{ 
         mainImage.image = [UIImage imageNamed:imageName]; 
        } 
        completion:NULL]; 
} 

此外,您可以使用enum作爲常量以提高可讀性。這些只是順序整數。

typedef enum { 
    MyClassPlayerVoiceYellow = 0, 
    MyClassPlayerVoiceOrange, 
    MyClassPlayerVoiceRed, 
    MyClassPlayerVoicePink, 
} MyClassPlayerVoice; 

然後在交換機中使用它們:

switch (index) { 
    case MyClassPlayerVoiceYellow: 
     imageName = @"yellow"; // You don't need the ".png" part. 
     break: 
    case MyClassPlayerVoiceOrange: 
     imageName = @"orange"; 
     break: 
    case MyClassPlayerVoiceRed: 
     imageName = @"red"; 
     break: 
    case MyClassPlayerVoicePink: 
     imageName = @"pink"; 
     break: 
    default: 
     break: 
} 
+0

嘿戴夫有啥不工作是開關的表達。我不太確定該怎麼設置它。 SoundEmotions是我的AVPlayerQueue(soundQueue)中PlayerItem的數組,基本上我想知道哪個項目正在播放,以便我可以將它的圖像切換到其各自的軌道。 – KingPolygon 2013-03-11 20:41:56

+0

謝謝。我已經更新了我的答案。 – 2013-03-11 21:49:46

+0

非常感謝你澄清這一點! – KingPolygon 2013-03-12 03:20:29