当我按下按钮,然后按另一个按钮时,声音重叠。如何解决这个问题,以便在按下另一个声音时第一个声音停止?
- (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];
}
答案 0 :(得分:0)
在viewController的标题中声明你的AVAudioPlayer(每次播放声音时不要分配新的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];
}
也一定要阅读Apple Docs
答案 1 :(得分:0)
您可以执行以下操作:
(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];
}
答案 2 :(得分:0)
您发布的代码只播放一个声音,即发送给该方法的第一个声音。
如果您只想播放一个可更改的声音,请在停止播放器后释放音频,然后设置新声音。
答案 3 :(得分:0)
在你的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];
}
}
答案 4 :(得分:0)
添加一项检查以确定玩家是否已在每种方法的开头播放:
if (theAudio.playing == YES) {
[theAudio stop];
}
答案 5 :(得分:0)
您需要对BOOL值进行utizilize以使其正常工作。
你的.m文件中的在@implementation之前放了这个:
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;
}
}
老实说,这就是它。当按下另一个按钮时,它将停止声音。