1

Sir, What do you think the error of my Code.. because i cant record a Audio. can you help me in my project? i want to make a simple Recording Project. with three Buttons (PLAY, STOP, RECORD)...by the way i didnt use the nib file. im newbie in Objective-C my approach is purely Programmatically..Thanks in advance more power..

and this is my code in 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];
        }
}
4

2 回答 2

0

Apple 提供了一个名为的示例应用程序SpeakHere,它将极大地帮助您使用音频文件服务来创建、录制到CAF(核心音频格式)音频文件并从中读取。

您可以在 Apple 的开发者网站上找到

希望这可以帮助。

于 2012-07-16T13:08:07.630 回答
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 重量很大,但请注意,最低质量是最糟糕的东西已知的星系):

  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];
      }

请注意,我完全无视内存管理,这篇文章不是内存管理指南。

于 2012-07-16T13:26:42.240 回答