Reputation: 5
I have some multitouch questions
this is one part of my code in .m file
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self.view];
if(pt.x>634 && pt.x<733 && pt.y >64 && pt.y<145)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"si" ofType:@"mp3"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
if(pt.x>634 && pt.x<733 && pt.y >195 && pt.y<276)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"la" ofType:@"mp3"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSString *path = [[NSBundle mainBundle] pathForResource:@"rest" ofType:@"mp3"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
I hope this can work.
Actually,the second "if ((range) &&(range)) " part doesn't work. Can anyone tell me the solution?
The first range works, and I want the second range to play "la.mp3", but when 2 fingers touch down, it doesn't play any music.
Upvotes: 0
Views: 136
Reputation: 5953
You need to re-read the docs on MultiTouch Events. First, you need to set the multipleTouchEnabled property of the view to YES. Secondly, you're only getting one touch event each time through, as you're setting
UITouch *touch = [touches anyObject];
Instead try
for (UITouch * touch in touches) {
to execute your code on each touch in the current set.
Secondly, I'm concerned that your AVAudioPlayer will be released upon the termination of TouchDidBegin; I would think this would cancel the rest of the playback. It should probably be a property. Also, AVAudioPlayer can only play one audio track at a time, so you'll want to have two different ones hanging around.
Another (better) alternative would be to ditch touches altogether and just use buttons. You can be notified when a button is pressed down (starting a note) and when a button is released (keep track of which ones are still down, and if you're at the last one down, then play the "rest.mp3").
Upvotes: 0
Reputation: 33421
Sorry, this is going to be a complete edit of the answer since I can't manage to delete the last one. I made a major reading error and I need to write a new answer. You must not use [touches anyObject]
because that will not reliably give you the touch you want in a multitouch system.
You must use [[touches allObjects] objectAtIndex:#]
to extract the touch. The touches will be in there in the order they came.
Upvotes: 1