在AVQueuePlayer中使用Switch语句?

时间:2013-03-11 20:21:37

标签: objective-c ios6 switch-statement avaudioplayer avqueueplayer

我有一个由四个轨道组成的队列。当曲目改变时,我想改变UIImage,关于该特定曲目(如果曲目1正在播放我想要显示标题为1.png的图像,如果曲目2正在播放我想要显示2.png等)

我想使用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;
    }

1 个答案:

答案 0 :(得分:2)

switch语句需要一个整数。在这种情况下,您想要的整数是当前播放的AVPlayerItem的索引。

因此,请将传递AVPlayerItem数组的数组副本保存到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];
}

此外,您可以使用枚举作为常量以提高可读性。这些只是连续的整数。

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:
}