2011-10-30 34 views
0

我必須錄製與「Talking tom」完全相似的應用視頻。 從HereHere接受幫助,我已經捕獲屏幕並使用這些圖像製作了視頻,但沒有任何聲音。錄製視頻像「Talking tom」iphone(添加音頻時發出的問題)

我已分別記錄聲音和視頻文件,但不知道如何將它們添加

誰能告訴我怎麼將聲音添加到該視頻如何與聲音記錄下來。

任何人都可以幫忙嗎?

+0

可能重複[如何記錄屏幕視頻就像Talking Tomcat應用程序在iPhone中所做的一樣?](http://stackoverflow.com/questions/6980370/how-to-record-screen-video-as-like-talking -tomcat-application-does-in-iphone) – duskwuff

+1

是的,是類似的(但不重複),也有提到,在我的鏈接....請閱讀完整的問題之前downvoting ...... 問題是加入聲音.... – sajwan

+0

你有沒有得到這個..的解決方案? – Rajneesh071

回答

0

iOS應用程序無法真正記錄(使用任何公共API)其自身產生的聲音。一個應用可以做的是生成兩次相同的音頻,一個用於播放,一個用於流式傳輸到文件。你必須堅持只有你知道如何做到這兩種方式的聲音,如將PCM波形複製到緩衝區等。

一旦你有你的音頻樣本的重複緩衝區,應該有如何發送它的示例代碼到AVAssetWriter。

+0

是的,我在問什麼...我已經創建了2個獨立的文件中的視頻和聲音,但我不知道如何加入他們 – sajwan

1
-(void) processVideo: (NSURL*) videoUrl{ 
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL: videoUrl options:nil]; 

AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

NSError * error = nil; 

for (NSMutableDictionary * audioInfo in appDelegate.audioInfoArray) 
{ 
    NSString *pathString = [[NSHomeDirectory() stringByAppendingString:@」/Documents/」] stringByAppendingString: [audioInfo objectForKey: @」fileName」]]; 

    AVURLAsset * urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:pathString] options:nil]; 

    AVAssetTrack * audioAssetTrack = [[urlAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio 
                        preferredTrackID: kCMPersistentTrackID_Invalid]; 

    NSLog(@」%lf」, [[audioInfo objectForKey: @」startTime」] doubleValue]); 

    CMTime audioStartTime = CMTimeMake(([[audioInfo objectForKey: @」startTime」] doubleValue]*TIME_SCALE), TIME_SCALE); 

    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,urlAsset.duration) ofTrack:audioAssetTrack atTime:audioStartTime error:&error];  
} 


AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                       preferredTrackID:kCMPersistentTrackID_Invalid]; 
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
           ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
           atTime:kCMTimeZero error:nil]; 

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                     presetName:AVAssetExportPresetPassthrough]; 

NSString* videoName = @」export.mov」; 

NSString *exportPath = [[self pathToDocumentsDirectory] stringByAppendingPathComponent:videoName]; 
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
{ 
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; 
} 

_assetExport.outputFileType = @」com.apple.quicktime-movie」; 
NSLog(@」file type %@」,_assetExport.outputFileType); 
_assetExport.outputURL = exportUrl; 
_assetExport.shouldOptimizeForNetworkUse = YES; 

[_assetExport exportAsynchronouslyWithCompletionHandler: 
^(void) { 
    switch (_assetExport.status) 
    { 
     case AVAssetExportSessionStatusCompleted: 
      //export complete 
      NSLog(@」Export Complete」); 
      //[self uploadToYouTube]; 

      break; 
     case AVAssetExportSessionStatusFailed: 
      NSLog(@」Export Failed」); 
      NSLog(@」ExportSessionError: %@」, [_assetExport.error localizedDescription]); 
      //export error (see exportSession.error) 
      break; 
     case AVAssetExportSessionStatusCancelled: 
      NSLog(@」Export Failed」); 
      NSLog(@」ExportSessionError: %@」, [_assetExport.error localizedDescription]); 
      //export cancelled 
      break; 
    } 
}]; } 

你的電影文件(ie.without音頻)剛分配到NSURL並將它傳遞給上述ProcessVideo method.Then只是在audioInfoArray添加您的聲音文件(您想與您的視頻進行合併)在別的地方該程序在調用processVideo方法之前。然後它會將您的音頻與您的視頻文件合併。

您還可以根據audioinfoArray中按鍵「startTime」下分配的值決定視頻在視頻中播放的位置。使用Switch Case,您可以根據自己的願望播放視頻,上傳到Facebook等。