in .h file I wrote like this:
#import <UIKit/UIKit.h>
#import "SCListener.h"
#import <AVFoundation/AVFoundation.h>
@interface TalkingAndSayingViewController : UIViewController<AVAudioPlayerDelegate>
{
IBOutlet UIImageView *bkg;
IBOutlet UILabel *showVoulme;
SCListener *listener;
NSTimer *time;
BOOL listen;
NSString *audioPath;
AVAudioRecorder *recoder;
AVAudioPlayer *player;
int breakCount;
BOOL timeActive;
}
@property(nonatomic,assign)int roleIndex;
@end
and in the .m file I wrote this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
audioPath = [[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"audio.wav"];
[audioPath retain];
listen = YES;
NSMutableDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSString *imgName = [NSString stringWithFormat:@"role%d.png",self.roleIndex];
[bkg setImage:[UIImage imageNamed:imgName]];
recoder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioPath] settings:recordSettings error:nil];
[recoder setMeteringEnabled:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:nil];
[player setNumberOfLoops:0];
AVAudioSession *audioSession = [[AVAudioSession sharedInstance] retain];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive:YES error: nil];
time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(CheckVolume) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
timeActive = YES;
listen = YES;
NSLog(@"fihish playing, start listen");
}
-(void)CheckVolume
{
Float32 peak = [[SCListener sharedListener] peakPower];
if(timeActive)
{
if(listen)
{
[showVoulme setText:[NSString stringWithFormat:@"%f",peak]];
if(peak>0.3)
{
listen = NO;
[recoder prepareToRecord];
[recoder record];
NSLog(@"start recording");
}
}
else
{
if(peak<0.1)
{
if(breakCount >= 5)
{
NSLog(@"start play");
[recoder stop];
player.delegate = self;
[player prepareToPlay];
[player play];
timeActive = NO;
breakCount = 0;
}
else
breakCount++;
}
else
breakCount = 0;
}
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[recoder stop];
[[SCListener sharedListener] stop];
timeActive = NO;
}
-(void)viewDidAppear:(BOOL)animated
{
timeActive = YES;
[[SCListener sharedListener] listen];
breakCount = 0;
}
Here have some problems, first:audioPlayerDidFinishPlaying this function didn't do at all, and this code only can run to "start play", but the sound didn't play in fact, I used to use the recorder to check the volume when it's in the recording condition, but the peakPowerForChannel:0 only returns a static value of 0.00001, I have no idea to solve this problem, anyone can help me or send me a sample code of auto record and auto play? I really appreciate it. Thanks
Yeah, already 5 years past, and I still work with iOS dev, this problem been resolved in about one month after this question ask, and I never remembered to see this answer. I had made an app like talking tom just then. And already finished it, however, useless for me.