2012-02-22 77 views
1

我浏览了与该主题相关的各种帖子,但答案并没有真正的帮助。我用this教程来实现音频文件的录制和播放。似乎缺少的是如何永久保存记录。当我退出我的应用程序时,声音文件就在那里,但没有任何内容。我甚至不知道它是保存rocerd还是只创建文件。 下面是一个代码示例:xcode:如何使用AVFoundation录制音频后保存音频文件

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

tempRecFile = [NSURL fileURLWithPath:soundFilePath]; 

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

recorder = [[AVAudioRecorder alloc] initWithURL:tempRecFile settings:recSettings error:nil]; 
[recorder setDelegate:self]; 
[recorder prepareToRecord]; 
[recorder record]; 

我看到一个可能的解决了这个问题here但因为我是新来的iOS我真的不明白它或者它只是没有为我工作。

请帮助并提供一个代码示例。我阅读了有关AVFoundation和其他课程的文档,但没有获得积极的结果。

SOS :)

UPDATE 这是我的记录方法:

-(IBAction)recording{ 
    //***** METHOD for recording 

    if(isNotRecording){ 
     isNotRecording = NO; 
     [recButton setTitle:@"Stop" forState:UIControlStateNormal]; 
     recStateLabel.text = @"Recording"; 

     [recorder setDelegate:self]; 
     [recorder prepareToRecord]; 
     [recorder record]; 

    } 
    else{ 
     isNotRecording = YES; 
     [recButton setTitle:@"Rec Begin" forState:UIControlStateNormal]; 
     playButton.hidden = NO; 
     recStateLabel.text = @"Not recording"; 
     [recorder stop]; 
    } 


} 

这是我的viewDidLoad:

- (void)viewDidLoad 
{ 
    //record sound test code - start 
    isNotRecording = YES; 
    //playButton.hidden = YES; 
    recStateLabel.text = @"Not recording"; 

    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

    [audioSession setActive:YES error:nil]; 
    //record sound test code - END 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    // code for generating the sound file that is to be saved 
    NSArray *dirPaths; 
    NSString *docsDir; 

    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; 

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

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

    } else { 
     [recorder prepareToRecord]; 
    } 

} 

这是我的播放方法:

-(IBAction)playback{ 
    //AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil]; 
    NSError *error; 
    // setting the uri of the formed created file 
    NSArray *dirPaths; 
    NSString *docsDir; 



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

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL error:&error]; 



[player setNumberOfLoops:0]; 
[player prepareToPlay]; 
[player setVolume:1]; 
[player play]; 
NSLog(@"URL je: %@", soundFileURL); 


//method 2 
if (error) 
    NSLog(@"Error: %@", 
      [error localizedDescription]); 
else 
{ 

    [player play]; 
    if(player.isPlaying==YES) 
    { 
     NSLog(@"It's playing"); 
    } 
} 

}

这是我明白我做了什么。 任何建议,我可能是错的?

回答

2

你需要停止以将数据保存到永久文件记录:

[recorder stop]; 
+0

你好@mahboudz,我用[记录器停止]停止记录。我不认为这是问题。停止录制后,我可以使用与录音机具有相同网址的AVplayer播放“已录制”声音。问题是,我重新启动我的应用程序后声音没有播放。 Taht表示它不是永久保存的,但仅在会话/实例期间保存。 – iPhoneNoob 2012-02-22 00:20:04

+0

您是否在停止录制后说文件长度为零? – mahboudz 2012-02-22 00:48:59

+0

您是否实现了委托方法:' - (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)记录器错误:(NSError *)错误'?这可以告诉你加密时是否有错误。你什么时候叫'-stop'?它是在什么时候将应用程序放入后台?或之前? – mahboudz 2012-02-22 00:50:28

1

仅供参考 - 在“[录音prepareToRecord]”方法创建一个新的音频文件,覆盖任何之前的记录文件。

3

问题是,当你告诉XCode(在viewDidLoad方法中)到[recorder prepareToRecord];它摆脱了以前录制的音频文件。消除“if”语句中的那段代码,并且当您退出并返回应用程序时,您的音频文件将被保存。