【发布时间】:2011-10-04 15:38:46
【问题描述】:
我正在尝试做一个应用程序,当使用该应用程序的人在屏幕上拖动他/她的手指时,应该播放一个简短的声音样本。当手指离开屏幕时 - 声音将停止。
这是触发声音的函数:
-(IBAction) playSound:(id)sender{
NSString *soundPath = [[NSBundle mainBundle]pathForResource:@"sound" ofType:@"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundPath];
newPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
newPlayer.numberOfLoops = -1;
[newPlayer play];
}
这是停止声音的功能:
-(IBAction) stopSound{
if ([newPlayer isPlaying]) {
[newPlayer stop];
}
[newPlayer release];
}
我最开始使用的是 Touch Down 事件,它可以无缝地循环播放声音。此时,stopSound 与“Touch Up Inside”事件协同工作。
正如我所说,关键是当用户在屏幕上拖动他/她的手指时应该会产生声音。但是,当我尝试将 playSound 功能与“Touch Drag Inside”联系起来时,声音会在自身上反复循环,而不是一次循环,这让它很难使用。更改为拖动事件后,stopSound 功能不起作用。
我尝试使用 NSTimer 创建某种方式来处理循环,但没有任何成功。
【问题讨论】:
标签: iphone objective-c audio touch drag