2011-08-22 38 views
0

我需要用iPhone麥克風錄製音頻流。 我從這個東西使用AVAudioRecorder。AVAudioRecorder&調試器停止,無信號

我的init AVAR:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *cachePath = [[paths objectAtIndex:0] retain]; 
NSError *error; 
BOOL isDir = NO; 
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) { 
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error]; 
} 

pathNewFile= [cachePath stringByAppendingPathComponent:@"123.caf"]; 

NSLog(@"%@",pathNewFile); 

NSDictionary *settings=[[NSDictionary alloc]initWithObjectsAndKeys: 
           [NSNumber numberWithFloat:44100.0f],AVSampleRateKey, 
            [NSNumber numberWithInt:kAudioFormatAppleLossless],AVFormatIDKey, 
            [NSNumber numberWithInt:1],AVNumberOfChannelsKey, 
            [NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil]; 
recorder=[[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:pathNewFile] settings:settings error:nil]; 
[recorder prepareToRecord]; 

我做起,當用戶按下REC鍵錄音:

-(void)recButton:(id)sender{ 
    NSLog(@"start rec"); 
     [rootNotesButton setTitle:@"Stop" forState:UIControlStateNormal]; 
      [rootNotesButton removeTarget:self action:@selector(recButton:) forControlEvents:UIControlEventTouchUpInside]; 
      [rootNotesButton addTarget:self action:@selector(stopButton:) forControlEvents:UIControlEventTouchUpInside]; 
      [recorder record]; 
} 

-(void)stopButton:(id)sender{ 
      NSLog(@"stop rec"); 
      [rootNotesButton setTitle:@"Play" forState:UIControlStateNormal]; 
      [rootNotesButton removeTarget:self action:@selector(stopButton:) forControlEvents:UIControlEventTouchUpInside]; 
      [rootNotesButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside]; 
      [recorder stop]; 
} 

-(void)playButton:(id)sender{ 
     NSLog(@"play rec"); 
      [rootNotesButton setTitle:@"Stop" forState:UIControlStateNormal]; 
      [rootNotesButton removeTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside]; 
      [rootNotesButton addTarget:self action:@selector(stopPButton:) forControlEvents:UIControlEventTouchUpInside]; 

      player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:pathNewFile] error:nil]; 
      [player play]; 
} 

-(void)stopPButton:(id)sender{ 
     NSLog(@"Stop play"); 
      [rootNotesButton setTitle:@"Play" forState:UIControlStateNormal]; 
      [rootNotesButton removeTarget:self action:@selector(stopPButton:) forControlEvents:UIControlEventTouchUpInside]; 
      [rootNotesButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside]; 
      [player stop]; 
} 

但是,當我嘗試在模擬器中運行我的項目:我的調試器停止on string: recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:pathNewFile] settings:settings error:nil];沒有任何信號或錯誤。

回答

0

該問題似乎是在模擬器中寫入cachePath。我想這和它的工作:

#if TARGET_IPHONE_SIMULATOR 
    NSString *cachePath = @"/tmp"; 
#else 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *cachePath = [[paths objectAtIndex:0] retain]; 
#endif 

你有幾個內存泄漏:

  1. 您保留cachePath但不釋放它。實際上沒有理由首先保留它。

  2. 您正在創建player+alloc/-initplayButton這意味着你擁有它但你不釋放它(至少不是在發佈的代碼中)。如果您將其定義爲property,則需要使用self.player = …才能正確保留和釋放它。

  3. 您正在創建settings+alloc/-init但未釋放它。沒有理由這樣做。你可以像這樣,而不是創建它:

NSDictionary *settings=[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithFloat:44100.0f],AVSampleRateKey, [NSNumber numberWithInt:kAudioFormatAppleLossless],AVFormatIDKey, [NSNumber numberWithInt:1],AVNumberOfChannelsKey, [NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil];

您需要保留pathNewFile因爲stringByAppendingPathComponent返回一個字符串自動釋放和你在你的類的其他方法使用它。

+0

謝謝。有用。 是的,我知道。這只是測試版本我的程序。我從音頻錄音的測試方法寫出來。在最後的依賴我修復了所有錯誤和錯誤。 感謝您的建議。 – Riddick