Question

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;
    }
Was it helpful?

Solution

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:
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top