使用滑动手势启动和停止音频

时间:2012-11-28 01:53:48

标签: ios

我正在为我女儿玩一个小应用程序,我在启动和停止声音方面遇到了一些问题。我现在已经对它进行了编码,当你从一个页面滑动到另一个页面时它会播放不同的声音,但它们会重叠。我试图找出如何停止前一个声音并开始下一个声音,因为页面来回滑动。这就是我所拥有的:

@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);

0 个答案:

没有答案