AVAudioPlayer not Stop playing on Touch Up Inside

248 views Asked by At

I have an app that has a piano built into it. Each key (Button1) when pressed down starts to play the note and when the user lifts their finger I want the note to stop playing. What happens is that the audio does not stop playing when the stop function is set to Touch Up Inside on the key (Button1). If I change the the Action from the piano key (Button1) to a different button (Button2) it stops playing as it should if I press the button 2.

.h

#import <AVFoundation/AVAudioPlayer.h>

@interface FifthViewController : UIViewController <AVAudioPlayerDelegate> {
    AVAudioPlayer *theNote;

}

.m

#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVFoundation.h>

//White
-(IBAction)note1w:(id)sender { //Touch Down

    [self whiteKey:self];
    [self downKey:self];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"white1" ofType:@"aif"];
    theNote = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theNote.delegate = self;
    theNote.currentTime = 0;
    [theNote play];
}

-(IBAction)stopMusic { //Touch Up Inside - This is the action that I change from the Button1 (The Piano Key) to Button2 (A Reg UIButton) and it works.

    [theNote stop];

}    
2

There are 2 answers

0
MBarton On BEST ANSWER

I ended up creating a work around to fix the problem. I already had a long press gesture recognizer in the app so I added to the recognizer state ended [self stopMusic]; and it ran fine. I couldn't replicate the problem in another app. It must be a conflict with other code that is being ran.

0
Trausti Kristjansson On

I tried your code in a project and it worked fine. I suspect you misconfigured the touchUp connection (perhaps connected touchUpOutside as I have accidentally done on occasion).