2012-07-16 23 views
1

先生,您認爲我的代碼錯誤是什麼......因爲我無法錄製音頻。你能幫我完成我的項目嗎?我想製作一個簡單的錄音工程。有三個按鈕(PLAY,STOP,RECORD)......順便說一句,我沒有使用nib文件。在Objective-C IM新手我的做法是純粹Programmatically..Thanks提前更多的權力..Objective C:AVFoundation Recording Issue ..

,這是我在viewDidLoad中的代碼()

-(void)viewDidLoad 
{ 
    [super viewDidLoad];{ 
     playButton.enabled = NO; 
    stopButton.enabled = NO; 

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0]; 
    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"]; 

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

    NSDictionary *recordSettings = [NSDictionary 
            dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithInt:AVAudioQualityMin], 
            AVEncoderAudioQualityKey, 
            [NSNumber numberWithInt:16], 
            AVEncoderBitRateKey, 
            [NSNumber numberWithInt: 2], 
            AVNumberOfChannelsKey, 
            [NSNumber numberWithFloat:44100.0], 
            AVSampleRateKey, 
            nil]; 

    NSError *error = nil; 

    audioRecorder = [[AVAudioRecorder alloc]initWithURL:soundFileURL settings:recordSettings error:&error]; 

    if (error) 
    { 
     NSLog(@"error: %@", [error localizedDescription]); 

    } 
    else 
    { 
     [audioRecorder prepareToRecord]; 
    } 

} 


-(void) recordButton:(UIButton *)sender 
{ 
     if (!audioRecorder.recording) 
     { 

      playButton.enabled = NO; 
      stopButton.enabled = YES; 
      [audioRecorder record]; 
      NSLog(@"Record"); 
     } 
} 


-(void)stop:(UIButton *)sender 
{ 
     stopButton.enabled = NO; 
     playButton.enabled = YES; 
     recordButton.enabled = YES; 

     if (audioRecorder.recording) 
     { 
      [audioRecorder stop]; 
      NSLog(@"Stop"); 
     } 
     else if (audioPlayer.playing) 
     { 
      [audioPlayer stop]; 
     } 
} 

-(void) playAudio:(UIButton *)sender 
{ 
    NSError *error; 
     if (!audioRecorder.recording) 
     { 
      stopButton.enabled = YES; 
      recordButton.enabled = NO; 
      NSLog(@"Play"); 
      if (audioPlayer) 
      { 


      audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error]; 

      audioPlayer.delegate = self; 
      } 
      if (error) 

      { NSLog(@"Error: %@", 
         [error localizedDescription]); 
      } 
      else 
       [audioPlayer play]; 
     } 
} 

回答

0

蘋果提供了一個名爲SpeakHere一個示例應用程序,這將大大幫助您使用音頻文件服務創建,錄製並讀取CAF(核心音頻格式)音頻文件。

您可以在Apple的開發人員網站here上找到它。

希望這會有所幫助。

+0

先生感謝您的信息..但我下載了..掃描,但仍然在即時遇到麻煩 – 2012-07-16 13:11:09

+0

這[教程](http://www.iphoneam.com/blog/index.php?title=使用iphone-to-record-audio-a-guide&more = 1&c = 1&tb = 1&pb = 1)可能會更有趣。最後是一個帶有完整解決方案的zip。 – werner 2012-07-16 13:15:38

+0

再次感謝該鏈接先生..你有相同的問題,但在.xib /(IBOutlet或IBAction)方法的教程?再次感謝 – 2012-07-16 13:17:46

0

首先,將代碼從viewDidLoad移出到viewDidAppear或函數調用。接下來,閱讀關於AVAudioSession。簡而言之,當您分別錄製或播放時,您希望將其類別更改爲AVAudioSessionCategoryRecord或AVAudioSessionCategoryPlay。

- (void)beginRecording { 

    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    NSError *err = nil; 
    [audioSession setCategory:AVAudioSessionCategoryRecord error:&err]; 
    if(err){ 
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); 
    return; 
    } 
    err = nil; 
    [audioSession setActive:YES error:&err]; 
    if(err){ 
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); 
    return; 
    } 
    if (audioSession.inputIsAvailable) { 
    if ([audioRecorder prepareToRecord]) { 
     [audioRecorder record]; 
    } 
    else { 
     UIAlertView *alert = 
     [[UIAlertView alloc] initWithTitle:@"Error!" 
           message:@"Could not begin recording" 
           delegate:nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
    } 
} 

- (void)stopRecording { 
    [audioRecorder stop]; 
} 

這些都是你需要開始記錄(在他們的工作對我來說是最起碼的最低參數,但你可以設置質量要作爲AppleLoseless權重一噸的任意值,但要注意分質量施** IEST在已知的星系的東西):

NSMutableDictionary *settings = [[[NSMutableDictionary alloc] init] autorelease]; 
    [settings setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey]; 
    [settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    [settings setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 
    [settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
    [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 
    [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; 

    NSURL *url = [NSURL fileURLWithPath:filePath]; 
    NSError *err = nil; 
    self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:url 
                 settings:settings 
                  error:&err]; 
    if(err){ 
     UIAlertView *alert = 
     [[UIAlertView alloc] initWithTitle:@"Warning" 
            message:[err localizedDescription] 
            delegate:nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
     } 

請注意,我完全不顧內存管理,這個帖子是不是內存管理指南。

+0

我會嘗試這個先生..謝謝你回答我的問題 – 2012-07-16 13:37:59