2015-05-05 114 views
13

如何將音頻片段實時上傳到服務器而錄製?基本上我的要求是上傳一個音頻剪輯作爲卡盤/數據包,而其錄音。 我已經使用IQAudioRecorderController https://github.com/hackiftekhar/IQAudioRecorderController做了錄音部分。它會記錄音頻並保存到TemporaryDirectory。在錄音時實時上傳音頻剪輯?

我想知道如何在不保存音頻剪輯的情況下實時上傳。

這是記錄部分

//Unique recording URL 


NSString *fileName = [[NSProcessInfo processInfo] globallyUniqueString]; 
_recordingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.m4a",fileName]]; 


// Initiate and prepare the recorder 

_audioRecorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:_recordingFilePath] settings:recordSetting error:nil]; 
_audioRecorder.delegate = self; 
_audioRecorder.meteringEnabled = YES; 


// Recording start 
- (void)recordingButtonAction:(UIBarButtonItem *)item 
{ 
if (_isRecording == NO) 
    { 
    _isRecording = YES; 

    //UI Update 
    { 
     [self showNavigationButton:NO]; 
     _recordButton.tintColor = _recordingTintColor; 
     _playButton.enabled = NO; 
     _trashButton.enabled = NO; 
    } 

    /* 
    Create the recorder 
    */ 
    if ([[NSFileManager defaultManager] fileExistsAtPath:_recordingFilePath]) 
    { 
     [[NSFileManager defaultManager] removeItemAtPath:_recordingFilePath error:nil]; 
    } 

    _oldSessionCategory = [[AVAudioSession sharedInstance] category]; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; 
    [_audioRecorder prepareToRecord]; 
    [_audioRecorder record]; 
} 
else 
{ 
    _isRecording = NO; 

    //UI Update 
    { 
     [self showNavigationButton:YES]; 
     _recordButton.tintColor = _normalTintColor; 
     _playButton.enabled = YES; 
     _trashButton.enabled = YES; 
    } 

    [_audioRecorder stop]; 
    [[AVAudioSession sharedInstance] setCategory:_oldSessionCategory error:nil]; 
} 
} 

// Recording done 

-(void)doneAction:(UIBarButtonItem*)item 
{ 
    if ([self.delegate respondsToSelector:@selector(audioRecorderController:didFinishWithAudioAtPath:)]) 
    { 
     IQAudioRecorderController *controller = (IQAudioRecorderController*)[self navigationController]; 
     [self.delegate audioRecorderController:controller didFinishWithAudioAtPath:_recordingFilePath]; 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

回答

1

有解決這個的各種方式,一種方式是創建自己的AudioGraph。 AudioGraph可以從麥克風或文件中獲取樣本。然後繼續輸出單位,但安裝回調以獲取採樣的幀。然後你將這些數據推送到你的網絡類,然後可以逐個數據包地上傳數據包

一個很好的例子,說明如何將這些捕獲的數據包寫入磁盤是AVCaptureAudioDataOutput

在這個例子中,數據包被寫入起訴ExtAudioFileWriteAsync。您必須將其替換爲您自己的上傳到服務器的邏輯。請注意,儘管您可以輕鬆地做到這一點,但一個問題是,這會爲您提供原始音頻樣本。如果您需要將它們作爲波形文件或類似文件,則可能需要等待直到錄製完成,因爲文件的標題需要關於包含的音頻樣本的信息。

-1

如果您想在錄製完成後上傳錄製文件,您正在使用的代碼將適用於您,因爲它會爲您提供最終錄製的文件。

如果您想上傳現場錄音到那麼我認爲你必須去與組合的服務器,

AudioSession記錄的東西

ffmpeg上傳直播音頻服務器。

你可以錄製音頻和here

管理音頻緩衝區進行的ffmpeg我認爲你有李爾有很多很好的幫助。使用ffmpeg將靜態/保存的音頻文件發送到服務器將很容易,但將現場音頻緩衝區發送到服務器將是一件棘手的工作。

+0

您不需要'ffmpeg'來上傳音頻,而且您可能會將App中內置的應用程序放在App Store上發生許可問題。 – damian

+0

ffmpeg可能還有其他選項,但是如果您關心授權,那麼將應用程序提交給App Store是沒有問題的,因爲我成功地向App Store提交了一個包含ffmpeg實現的應用程序,但是您必須提及您正在使用它您的應用程序的信用目的。 – Yuvrajsinh

+0

ffmpeg獲得LGPL許可。 LGPL的條款與App Store不兼容。基本上,你違反了ffmpeg許可條款。 https://trac.ffmpeg.org/ticket/1229 – damian