2017-08-12 79 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鏈接你沒有存儲任何的音頻文件在你的核心數據做。您可以存儲在本地目錄中,然後使用後臺線程上傳到服務器。

相關問題