2012-07-16 46 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