Iphone 如何停止AVFoundation中的声音?
我已经做了一个iPhone应用程序,我按下一个按钮,声音就会播放。但当我再次按下它时,我希望它停止。我有很多声音,但我只想在按下两次时停止这个声音 这是我使用的代码Iphone 如何停止AVFoundation中的声音?,iphone,objective-c,avfoundation,Iphone,Objective C,Avfoundation,我已经做了一个iPhone应用程序,我按下一个按钮,声音就会播放。但当我再次按下它时,我希望它停止。我有很多声音,但我只想在按下两次时停止这个声音 这是我使用的代码 -(IBAction)pushBeat { NSString *path =[[NSBundle mainBundle] pathForResource:@"beat1" ofType:@"mp3"]; AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] ini
-(IBAction)pushBeat {
NSString *path =[[NSBundle mainBundle]
pathForResource:@"beat1" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
我对xcode了解不多,所以这可能是错误的,如果是,请告诉我
似乎“音频”应该是类成员,而不是函数的本地成员,C++中你会这样做(同样,因为我不知道Xcode这是伪代码)。
也许它不会帮助你,但它可以帮助其他人 您应该做的是一个类变量 在class.h中,生成一个AVAudioPlayer变量:
{
AVAudioPlayer *actualAudioPlayer_;
}
@property(nonatomic, retain) AVAudioPlayer *actualAudioPlayer;
在class.m中,使其sythesize和dealloc,然后更改方法:
-(IBAction)pushBeat {
NSString *path =[[NSBundle mainBundle]
pathForResource:@"beat1" ofType:@"mp3"];
[self.actualAudioPlayer stop]; //NEW
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.actualAudioPlayer = theAudio; //NEW
[sel.actualAudioPlayer play]; //NEW
[theAudio release]; //NEW
}
希望这对别人有帮助
-(IBAction)pushBeat {
NSString *path =[[NSBundle mainBundle]
pathForResource:@"beat1" ofType:@"mp3"];
[self.actualAudioPlayer stop]; //NEW
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.actualAudioPlayer = theAudio; //NEW
[sel.actualAudioPlayer play]; //NEW
[theAudio release]; //NEW
}