1

我正在开发一个小应用程序供我女儿玩,但我在启动和停止声音时遇到了一点问题。我现在已经对其进行了编码,当您从一页滑动到下一页时它会播放不同的声音,但它们会重叠。我试图弄清楚如何在页面来回滑动时停止上一个声音并开始下一个声音。这是我所拥有的:

@implementation ViewController
@synthesize imageview;

int imageIndex = 0;

- (IBAction)handleSwipe:(UIGestureRecognizer *)sender {
//NSLog(@"swiped");


UISwipeGestureRecognizerDirection direction =
[(UISwipeGestureRecognizer *) sender direction];

switch (direction) {
    case UISwipeGestureRecognizerDirectionLeft:
        imageIndex++;
        break;
    case UISwipeGestureRecognizerDirectionRight:
        imageIndex--;
        break;
    default:
        break;
}
[self doit];
}


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imageIndex = -1;

imageview.image = [UIImage imageNamed:@"icon.png"];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void) doit {
NSArray *images= [[NSArray alloc] initWithObjects:
                  @"bee.png",
                  @"cow.png",
                  @"cat.png",
                  @"dog.png",
                  @"elephant.png",
                  @"goat.png",
                  @"horse.png",
                  @"lion.png",
                  @"monkey.png",
                  @"pig.png",
                  @"rooster.png",
                  @"sheep.png",
                  @"snake.png",
                  @"squirrel.png",
                  @"tiger.png",
                  @"wolf.png", nil];

NSArray *audio= [[NSArray alloc] initWithObjects:
                 @"Bee",
                 @"Cow",
                 @"Cat",
                 @"Dog",
                 @"Elephant",
                 @"Goat",
                 @"Horse",
                 @"Lion",
                 @"Monkey",
                 @"Pig",
                 @"Rooster",
                 @"Sheep",
                 @"Snake",
                 @"Squirrel",
                 @"Tiger",
                 @"Wolf", nil];
imageIndex = (imageIndex < 0) ? ([images count] -1):
imageIndex % [images count];
imageview.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];


//audio here
NSString *str = [audio objectAtIndex:imageIndex];
CFStringRef string =  (__bridge CFStringRef) str;

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundeFileURLRef;
soundeFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) string, CFSTR ("mp3"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundeFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
4

0 回答 0