2011-01-10 70 views
7

有什麼方法可以記錄到音頻文件的末尾?我們不能暫停記錄而不是停止記錄,因爲用戶需要稍後能夠回到應用程序,並將更多音頻添加到他們的記錄中。目前,音頻以NSData的形式存儲在CoreData中。 NSData的AppendData不起作用,因爲生成的音頻文件仍然報告它只與原始數據一樣長。AVAudioRecorder/AVAudioPlayer - 追加記錄到文件

另一種可能性是將原始音頻文件和新音頻文件一起,並將它們連接成一個音頻文件,如果有任何方法可以這樣做的話。

+0

看看我的代碼如下。這是我在生產應用程序中使用的。 – memmons 2013-05-02 16:52:17

+0

你可以找到我的完整代碼進行評估:https://stackoverflow.com/a/49136633/2244094 – 2018-03-06 17:31:34

回答

1

我沒有完整的代碼示例,但擴展音頻文件服務可以幫助您連接兩個音頻文件。在Xcode中搜索擴展音頻文件服務或訪問下面的鏈接。

Apple documentation

+0

該文檔中沒有任何內容引用附加音頻文件。 – memmons 2013-05-03 15:55:05

4

您可以通過添加兩個文件和導出使用AVAssetExportSessionexportAsynchronouslyWithCompletionHandler方法組成後創建AVMutableCompositionTrack追加兩個音頻文件。

請參閱下面的鏈接瞭解更多詳情。

AVAssetExportSession Class Reference

Creating New Assets

希望這有助於解決您的問題。

6

這可以使用AVMutableComposionTrack insertTimeRange:ofTrack:atTime:error相當容易地完成。代碼有點冗長,但它實際上就像4個步驟:

// Create a new audio track we can append to 
AVMutableComposition* composition = [AVMutableComposition composition]; 
AVMutableCompositionTrack* appendedAudioTrack = 
    [composition addMutableTrackWithMediaType:AVMediaTypeAudio 
          preferredTrackID:kCMPersistentTrackID_Invalid]; 

// Grab the two audio tracks that need to be appended 
AVURLAsset* originalAsset = [[AVURLAsset alloc] 
    initWithURL:[NSURL fileURLWithPath:originalAudioPath] options:nil]; 
AVURLAsset* newAsset = [[AVURLAsset alloc] 
    initWithURL:[NSURL fileURLWithPath:newAudioPath] options:nil]; 

NSError* error = nil; 

// Grab the first audio track and insert it into our appendedAudioTrack 
AVAssetTrack *originalTrack = [originalAsset tracksWithMediaType:AVMediaTypeAudio]; 
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, originalAsset.duration); 
[appendedAudioTrack insertTimeRange:timeRange 
          ofTrack:[originalTrack objectAtIndex:0] 
          atTime:kCMTimeZero 
           error:&error]; 
if (error) 
{ 
    // do something 
    return; 
} 

// Grab the second audio track and insert it at the end of the first one 
AVAssetTrack *newTrack = [newAsset tracksWithMediaType:AVMediaTypeAudio]; 
timeRange = CMTimeRangeMake(kCMTimeZero, newAsset.duration); 
[appendedAudioTrack insertTimeRange:timeRange 
          ofTrack:[newTrack objectAtIndex:0] 
          atTime:originalAsset.duration 
           error:&error]; 

if (error) 
{ 
    // do something 
    return; 
} 

// Create a new audio file using the appendedAudioTrack  
AVAssetExportSession* exportSession = [AVAssetExportSession 
             exportSessionWithAsset:composition 
             presetName:AVAssetExportPresetAppleM4A]; 
if (!exportSession) 
{ 
    // do something 
    return; 
} 


NSString* appendedAudioPath= @""; // make sure to fill this value in  
exportSession.outputURL = [NSURL fileURLWithPath:appendedAudioPath]; 
exportSession.outputFileType = AVFileTypeAppleM4A; 
[exportSession exportAsynchronouslyWithCompletionHandler:^{ 

    // exported successfully? 
    switch (exportSession.status) 
    { 
     case AVAssetExportSessionStatusFailed: 
      break; 
     case AVAssetExportSessionStatusCompleted: 
      // you should now have the appended audio file 
      break; 
     case AVAssetExportSessionStatusWaiting: 
      break; 
     default: 
      break; 
    } 
    NSError* error = nil; 

}];