我已经在我们的项目中实现了录音功能,但我无法控制停止和丢弃按钮。第二次点击放弃按钮后无法录制声音。单击丢弃后,音频文件也正在播放并且无法再次单击开始按钮。
请帮忙解决这个问题
这是源代码
.h file
@interface VoiceInput : UIViewController
<AVAudioRecorderDelegate>
{
//Audio record
float remainingDelayTime;
float remainingRecordTime;
UILabel *delayLabel;
UIProgressView *progressView;
AVAudioRecorder *recorder;
NSTimer *delayTimer;
NSTimer *recordTimer;
BOOL toggle;
NSURL *recordedTmpFile;
NSError *error;
}
@property (nonatomic, retain) AVAudioRecorder *recorder;
@property (nonatomic, retain) NSTimer *delayTimer;
@property (nonatomic, retain) NSTimer *recordTimer;
@end
.m file
@implementation VoiceInput
@synthesize progressView;
@synthesize recorder;
@synthesize delayTimer;
@synthesize recordTimer;
- (void)viewDidLoad
{
toggle = YES;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[audioSession setActive:YES error:&error];
}
-(void)startPushed
{
if (toggle)
{
remainingDelayTime = 4.0;
delayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(delayTimerFired:)
userInfo:nil
repeats:YES];
toggle = NO;
NSMutableDictionary *rs = [[NSMutableDictionary alloc] init];
[rs setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[rs setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[rs setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
NSLog(@"USING FILE CALLED: %@", recordedTmpFile);
recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:rs error:&error];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}
else
{
toggle = YES;
NSLog(@"Using File Called: %@", recordedTmpFile);
[recorder stop];
}
self.discardButton.enabled = NO;
self.startButton.enabled = NO;
self.stopButton.enabled = YES;
}
-(void)stopPushed
{
if([self.recorder isRecording])
{
[self.recorder stop];
if(remainingRecordTime >= 1.0)
{
[self.recordTimer invalidate];
}
}
self.delayLabel.textColor = [UIColor darkGrayColor];
self.delayLabel.text = [[NSString alloc] initWithFormat:@"Record in ..."];
self.discardButton.enabled = YES;
self.playbackButton.enabled = YES;
self.startButton.enabled = NO;
self.stopButton.enabled = NO;
}
-(void)playbackPushed
{
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
[avPlayer prepareToPlay];
[avPlayer play];
self.discardButton.enabled = YES;
self.playbackButton.enabled = YES;
self.returnButton.enabled = YES;
self.startButton.enabled = YES;
self.stopButton.enabled = YES;
}
-(void)discardPushed
{
[self.recorder deleteRecording];
self.progressView.progress = 0;
self.delayLabel.textColor = [UIColor darkGrayColor];
self.delayLabel.text = @"Record in ...";
self.discardButton.enabled = NO;
self.playbackButton.enabled = NO;
self.startButton.enabled = YES;
self.stopButton.enabled = YES;
}
-(void)delayTimerFired:(NSTimer *)theDelayTimer
{
self.progressView.progress = 0;
remainingDelayTime -= 1.0;
NSLog(@"fired %f", remainingDelayTime);
self.delayLabel.textColor = [UIColor blackColor];
self.delayLabel.text = [[NSString alloc] initWithFormat:@"Record in %2.0f",
remainingDelayTime];
if(remainingDelayTime <= 0.0)
{
[self.delayTimer invalidate];
self.delayLabel.text = [[NSString alloc] initWithFormat:@"Recording"];
[self.recorder recordForDuration:TIME];
remainingRecordTime = TIME;
recordTimer = [NSTimer scheduledTimerWithTimeInterval:TIME_DECREMENT
target:self
selector:@selector(recordTimerFired:)
userInfo:nil
repeats:YES];
}
}
-(void)recordTimerFired:(NSTimer *)theRecordTimer
{
remainingRecordTime -= TIME_DECREMENT;
NSLog(@"fired %f", remainingRecordTime);
self.progressView.progress = (TIME - remainingRecordTime)/TIME;
if(remainingRecordTime <= 0.0)
{
[self.recordTimer invalidate];
}
}
提前致谢