2012-06-16 37 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開發工作,這個問題在一個月左右已經解決了這個問題問之後,我從來沒有記得看到這個答案。我剛纔做了一個像湯姆一樣的應用程序。而且,已經完成了它,但是,對我來說毫無用處。

相關問題