【问题标题】:How to save audio file in core data and send to server?如何将音频文件保存在核心数据中并发送到服务器?
【发布时间】:2018-01-20 18:09:54
【问题描述】:

我正在创建录制音频并将该文件保存在设备中,然后我想发送到服务器。为此,我正在编写这样的代码

_playAudioOutlet.enabled = NO;
_stopRecording.enabled = NO;

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];

NSLog(@"%@",soundFilePath);
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;

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

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

if (error)
{
    NSLog(@"error: %@", [error localizedDescription]);
} else {
    [_audioRecorder prepareToRecord];
}
// to start recording...

- (IBAction)startRecording:(UIButton *)sender {

if (!_audioRecorder.recording)
{
    _playAudioOutlet.enabled = NO;
    _stopRecording.enabled = YES;
    [_audioRecorder record];
}

}

// To stop recording
- (IBAction)stopRecording:(UIButton *)sender {

_stopRecording.enabled = NO;
_playAudioOutlet.enabled = YES;
_startRecording.enabled = YES;

if (_audioRecorder.recording)
{
    [_audioRecorder stop];
} else if (_audioPlayer.playing) {
    [_audioPlayer stop];
}

}

// TO play audio

- (IBAction)playAudio:(UIButton *)sender {

if (!_audioRecorder.recording)
{
    _stopRecording.enabled = YES;
    _startRecording.enabled = NO;

    NSError *error;

    _audioPlayer = [[AVAudioPlayer alloc]
                    initWithContentsOfURL:_audioRecorder.url
                    error:&error];

    _audioPlayer.delegate = self;

    if (error)
        NSLog(@"Error: %@",
              [error localizedDescription]);
    else
        [_audioPlayer play];
}
}

我正在编写这样的代码......它工作正常。但是现在我想将该数据存储在核心数据中,并在保存我要发送的数据之后。为此,我创建了核心数据并使用实体名称:保存,属性名称:保存类型二进制数据。但我收到错误。 任何人都可以为此提供解决方案...

- (IBAction)stopRecording:(UIButton *)sender {

// Get conntext
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
context = appDelegate.persistentContainer.viewContext;

// Save data
NSManagedObject *newSong;
newSong = [NSEntityDescription
           insertNewObjectForEntityForName:@"Save"
           inManagedObjectContext:context];
NSError *error;

NSURL *urlOfRecording = _audioRecorder.url;
NSString *url = [urlOfRecording absoluteString];
NSLog(@"in saveRecording, the url is: %@",url);
[newSong setValue:url forKey:@"save"];
[context save:&error];
//    status.text = @"Song Saved!";

_stopRecording.enabled = NO;
_playAudioOutlet.enabled = YES;
_startRecording.enabled = YES;

if (_audioRecorder.recording)
{
    [_audioRecorder stop];
} else if (_audioPlayer.playing) {
    [_audioPlayer stop];
}

}

【问题讨论】:

    标签: ios objective-c cocoa-touch core-data avaudiorecorder


    【解决方案1】:

    根据this 链接,您不必在核心数据中存储任何音频文件。您可以存储在本地目录中,然后使用后台线程上传到服务器。

    【讨论】:

      【解决方案2】:

      Coredata 在使用 coredata 保存二进制数据时有一个“允许外部存储”选项,它会自动将大于 1mb 的文件存储到磁盘上。您已经将数据保存在应用程序文档字典中。因此,只需从文档目录中提取数据并使用 HTTP/UDP(无论协议如何)将其发送到您的服务器。 从目录中提取数据:

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

      【讨论】:

      • 好的,实际上我想复制该文件并用另一个名称重新保存。现在我想发送到服务器。如何重新保存文件...
      • 这不是真的。可以将音频文件保存到 Core Data。
      • 上一条评论中的名字是什么
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2016-12-11
      • 1970-01-01
      相关资源
      最近更新 更多