0

我有一个内置钢琴的应用程序。每个键(Button1)在按下时开始播放音符,当用户抬起手指时,我希望音符停止播放。发生的情况是,当停止功能设置为按键 (Button1) 上的 Touch Up Inside 时,音频不会停止播放。如果我将动作从钢琴键 (Button1) 更改为不同的按钮 (Button2),如果我按下按钮 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];

}    
4

2 回答 2

0

我在一个项目中尝试了你的代码,它运行良好。我怀疑您错误地配置了 touchUp 连接(可能连接了 touchUpOutside,因为我有时不小心这样做了)。

于 2014-01-13T04:03:29.457 回答
0

我最终创建了一个解决方法来解决问题。我已经在应用程序中有一个长按手势识别器,所以我添加到识别器状态结束 [self stopMusic]; 它运行良好。我无法在另一个应用程序中复制该问题。它必须与正在运行的其他代码发生冲突。

于 2014-01-17T09:00:38.500 回答