我是这样写的:
#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
在.m文件中我写了这个:
- (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;
}
这里有一些问题, 第一个:audioPlayerDidFinishPlaying这个功能根本没用,而且这个代码只能运行到“开始播放”,但声音实际上没有播放,我曾经使用录音机来检查音量是否处于录音状态,但是peakPowerForChannel:0只返回0.00001的静态值,我不知道解决这个问题,有谁可以帮我或者发给我一个自动记录和自动播放的示例代码?对此,我真的非常感激。谢谢
答案 0 :(得分:0)
是的,已经过了5年了,我仍然在使用iOS开发工具,这个问题在这个问题提出后大约一个月就得到了解决,而且我从来没有想过要看到这个答案。那时我做了一个像汤姆一样的应用程序。然而,已经完成它对我来说没用。