2017-08-12 71 views
0

我正在创建录制音频并将该文件保存在设备中,然后我要发送到服务器。为此我正在写这样的代码如何将音频文件保存在核心数据中并发送到服务器?

_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]; 
} 

} 

回答

1

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

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

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"]; 
NSData dataToBeSentInServer = [NSData dataWithContentsOfURL: soundFilePath]; 
+0

好吧,其实我想复制该文件,并保存与另一个名称。现在我想发送到服务器。如何重新保存文件... – iOS

+0

这是不正确的。音频文件可以保存到核心数据。 –

+0

https://stackoverflow.com/questions/14280989/renaming-an-existing-file-with-obj-c – Torongo

1

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

相关问题