Iphone 多个按键的声音重叠
当我按下一个按钮,然后按下另一个按钮时,声音重叠。我怎样才能解决这个问题,使第一个声音在按下另一个声音时停止Iphone 多个按键的声音重叠,iphone,ios4,memory,audio,Iphone,Ios4,Memory,Audio,当我按下一个按钮,然后按下另一个按钮时,声音重叠。我怎样才能解决这个问题,使第一个声音在按下另一个声音时停止 - (void)playOnce:(NSString *)aSound { NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"]; AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSUR
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
[theAudio release];
}
在viewController的标题中声明AVAudioPlayer(不要每次播放声音时都分配一个新的)。这样,您就可以在StopAudio方法中使用指针
@interface myViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}
@property (nonatomic, retain) AVAudioPlayer *theAudio;
@end
@implementation myViewController
@synthesize theAudio;
- (void)dealloc {
[theAudio release];
}
@end
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)stopAudio {
[theAudio stop];
[theAudio setCurrentTime:0];
}
@interface myViewController:UIViewController您可能可以执行以下操作:
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: @"caf"];
//stop audio player from playing so the sound don't overlap
if([theAudio isPlaying])
{
[theAudio stop]; //try this instead of stopAudio
}
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops: -1];
[theAudio setVolume: 1.0];
[theAudio play];
}
您发布的代码将只播放一个声音,即发送到该方法的第一个声音
如果您只想播放一个可更改的声音,请在停止播放机后,释放音频,然后设置新的声音。在playOnce方法中,未使用“path”变量--删除该变量以消除警告消息。你的playOnce没有为播放设置任何设置,所以我不确定该如何工作——除非你先调用playLooped?您还需要在initWithContentsOfUrl之后调用prepareToPlay
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
if (theAudio && [theAudio isPlaying]) {
[theAudio stop]
} else {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theAudio prepareToPlay];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops: 1];
[theAudio setVolume: 1.0];
[theAudio play];
}
}
在每个方法的开头添加一个检查,查看玩家是否已经在玩:
if (theAudio.playing == YES) {
[theAudio stop];
}
您需要使用BOOL值以使其正常工作
在@implementation之前的.m文件中,输入以下内容:
static BOOL soundIsPlaying = NO;
然后您的IBAction需要如下所示:
- (IBAction)play {
if (soundIsPlaying == YES) {
[theAudio release];
soundIsPlaying = NO;
}
else if (soundIsPlaying == NO) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"SOUNDFILENAME" ofType:@"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
theAudio.numberOfLoops = 0;
[theAudio play];
soundIsPlaying = YES;
}
}
说实话。当按下另一个按钮时,它将停止声音。我将如何在头文件中创建AVAudioPlayer我是新来的,不知道muchi添加了这些代码行,声音仍然重叠,只是上面编辑的代码。你能更详细地描述一下你想要实现的目标吗?我正在制作一个应用程序,它有20个按钮,每个按钮播放不同的声音,但当我单击其他按钮时,声音会重叠。然后在播放下一个声音之前停止AVAudioPlayer。你需要问一个问题。我的问题是,当我单击一个按钮时,声音会重叠播放声音,但我单击另一个声音,它们重叠。您可以执行以下操作之一:1。单击按钮时,检查AVaudioplayer是否仍在播放。如果是,您可以忽略它,不做任何事情,或者2您可以停止它,然后播放新声音。-(无效)playOnce:(NSString*)aSound;-(iAction)beatButton50{[self playOnce:@“racecars”];}它告诉我停止音频没有找到它很奇怪当我按下多个按钮时音频仍然重叠您是否交换了代码并确保[theAudio stop]调用?我如何交换代码我必须释放器它对编程来说是新的我仍在尝试学习如何释放音频并设置新的我删除了播放循环线,因为我并不真正需要它,我删除了路径线,但仍然没有声音播放和准备播放,你尝试了吗?你确实需要调用playLoop中的代码来设置AVAudioPlayer。我做到了,现在声音停止重叠,当我尝试按另一个不同声音的按钮时,它只会播放相同的声音啊,对,我明白你的意思了。我想你必须检查一下你是否有不同的声音,然后重新加载播放器。我有所有按钮的代码,但是我怎么看我是否有不同的声音,然后重新加载播放器呢?我是新编程的,所以我知道的不多