iphone - xcode:如何在使用 AVFoundation 录制音频后保存音频文件

标签 iphone ios xcode ipad avfoundation

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

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 和其他类的文档,但没有得到积极的结果。

紧急求救:)

更新 这是我的记录方法:

-(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");
    }
}

我希望这能让我清楚地知道我在做什么。 有什么我可能错的建议吗?

最佳答案

您需要停止记录才能将数据永久保存到文件中:

[recorder stop];

关于iphone - xcode:如何在使用 AVFoundation 录制音频后保存音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387237/

相关文章:

iphone - NSMutableData 保存到文件

ios - Delphi DX Seattle FMX 能否在具有 9.1 SDK 的同一个 .IPA 文件中在 IOS 8 和 IOS 9 上运行应用程序

iphone - 是否可以通过我的应用程序访问存储在 iPhone 上的崩溃日志?

iphone - 似乎不可能 : to animate the height change of row in UITableView

iOS - 将数据多次从父 View 传递到容器 View 的 View

ios - 在某些情况下尝试自动调整大小时,键盘扩展在 iOS 10 中丢失高度

iphone - 应用程序对 NSFetchRequests 没有响应

ios - 解析 SaveInBackgroundWithBlock 耗时较多

ios - 如何使用自动布局创建总宽度的百分比?

iphone - MKErrorDomain 错误 4 iPhone