2012-06-16 36 views
2

在.h文件中自动记录和自动播放我写这样的:关于使用AVAudioPlayer和AVAudioRecorder

#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.0000的静态值1,我不知道解决这个问题,任何人都可以帮我或给我一个自动记录和自动播放的示例代码?对此,我真的非常感激。由于

+0

我认为** ** audioPlayerDidFinishPlaying方法不叫,因为你设置** player.delegate =自我; **在很奇怪的地方。 更好地在播放器初始化后立即设置代表。 –

回答

0

呀,已经5年过去,我仍然与iOS开发工作,这个问题在一个月左右已经解决了这个问题问之后,我从来没有记得看到这个答案。我刚才做了一个像汤姆一样的应用程序。而且,已经完成了它,但是,对我来说毫无用处。

相关问题