0

I have a queue set up of four tracks. When the track changes, I want to change a UIImage, in regards to that specific track (If track 1 is playing I want to display an image titled 1.png, if track 2 is playing I want to display 2.png, etc).

I want to use a switch statement but I'm not sure how to use it when it comes to setting the expression.

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;
    }
KingPolygon
  • 4,753
  • 7
  • 43
  • 72

1 Answers1

2

The switch statement needs an integer. The integer you want in this case is the index of the current AVPlayerItem being played.

So, keep a copy of the array you passed an array of AVPlayerItems to the AVQueuePlayer. Then find the current player item in this array, and you'll have your index value.

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];
}

Also, you can use an enum for constants for better readability. These are just sequential integers.

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

Then use them in the switch:

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:
}
Dave Batton
  • 8,795
  • 1
  • 46
  • 50
  • Hey Dave so what's not working is the switch expression. I'm not quite sure what to set it too. SoundEmotions is the array of PlayerItem's in my AVPlayerQueue (soundQueue) and basically I want to know which item is playing so that I can switch it's image to its respective track. – KingPolygon Mar 11 '13 at 20:41