如何在Parse上存储iOS录音?

时间:2015-02-17 23:00:12

标签: ios audio parse-platform recording

新手iOS编码器,如果答案非常简单,请道歉。

所以我在viewDidLoad中设置录音

// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"MyAudioMemo.m4a",
                           nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];

我有一个记录新音频文件的小节按钮:

 // Stop the audio player before recording
if (player.playing) {
    [player stop];
}

if (!recorder.recording) {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];

    // Start recording
    [recorder record];

} else {

    // Pause recording
    [recorder pause];
}

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Stop" style:UIBarButtonItemStylePlain target:self
                                                                         action:@selector(stopTapped)];

然后开始变为停止按钮

[recorder stop];

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"New" style:UIBarButtonItemStylePlain target:self
                                                                         action:@selector(actionNew)];

如何将其添加为PFFile并保存在Parse中的字典中? 我已经阅读了很多Parse文档,但仍然没有真正掌握它。任何帮助非常感谢。

1 个答案:

答案 0 :(得分:0)

看起来最简单的解决方案是在用户完成录制时从手机加载文件,而不是尝试将录制内容直接保存到NSData对象。如果您想直接查看NSData个对象,请查看apple docs和此stackoverflow question

首先,您希望在用户完成录制后获得NSData的实例:

NSData *audioData = [[NSData alloc] initWithContentsOfFile:filePath];

制作新的PFFile:

PFFile *file = [PFFile fileWithName:@"resume.txt" data:data];

然后使用您想要的保存方法保存它。