首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >xcode:如何在使用AVFoundation录制音频后保存音频文件

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

Stack Overflow用户
提问于 2012-02-22 00:00:54
回答 3查看 17.1K关注 0票数 2

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

代码语言:javascript
运行
复制
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];

我看到了解决这个问题的可能方法,这里,但是由于我是iOS新手,所以我没有真正理解它,或者它对我没有用。

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

SOS :)

UPDATE这是我的录音方法:

代码语言:javascript
运行
复制
-(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:

代码语言:javascript
运行
复制
- (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];
    }

}

这是我的playBack方法:

代码语言:javascript
运行
复制
-(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");
    }
}

}

我跳š--这说明了我在做什么。有什么建议我可能错了吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-22 00:16:12

为了将数据永久保存到文件中,您需要停止记录:

代码语言:javascript
运行
复制
[recorder stop];
票数 3
EN

Stack Overflow用户

发布于 2013-07-11 20:03:07

问题是,当您告诉XCode (在viewDidLoad方法中)录制prepareToRecord时,它会删除先前录制的音频文件。删除"if“语句中的代码,当您退出并返回应用程序时,您的音频文件将被保存。

票数 3
EN

Stack Overflow用户

发布于 2013-04-22 20:12:56

FYI --“记录器prepareToRecord”方法创建一个新的音频文件,覆盖以前录制的任何文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9387237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档