开发者

How to play a sound when user touches a particular area on the screen

I am designing an app which consists of an image...

I am trying to play sound by using the following code...

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

UITouch *touch = [touches anyObject];


CGPoint point = [touch locationInView:self.view]; 
if (CGRectContainsPoint(CGRectMake(320,480,0,0),point));//CGRectMake(5, 5, 40, 130), point))
{
    AVAudioPlayer *player;
    if(!player){

        NSString* resourcePath = [[NSB开发者_C百科undle mainBundle] resourcePath];
        resourcePath = [resourcePath stringByAppendingString:@"/try.wav"];
        NSLog(@"Path to play: %@", resourcePath);
        NSError* err;

        //Initialize our player pointing to the path to our resource
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                  [NSURL fileURLWithPath:resourcePath] error:&err];

        if( err ){
            //bail!
            NSLog(@"Failed with reason: %@", [err localizedDescription]);
        }
        else{
            //set our delegate and begin playback
            player.delegate = self;
            [player play];
        }
    }

} 
}

In the above code the sound play when touch is detected anywhere on the screen. I want to play 5 different sounds when user touches differern place on the screen(say 1.wav when user touch between 100-200 in width and 50-100 in height). Can anyone please help me how to set the radius and also to play different sounds when touched in the radius specified...


CGRect area1 = CGRectMake(10,10, 10, 10);
CGRect area2 = CGRectMake(100,10, 10, 10);
CGRect area3 = CGRectMake(10,100, 10, 10);
CGRect area4 = CGRectMake(100,100, 10, 10);

if (CGRectContainsPoint(area1, point)) {
// play sound 1
} else ...
......
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜