Permalink
Browse files

update record sound"

  • Loading branch information...
tuo committed Feb 14, 2014
1 parent 5176663 commit 94dd95a650e076dd5c5a4e1be5e01020acd44928
Showing with 96 additions and 1 deletion.
  1. +1 −0 framework/Source/iOS/GPUImageMovieWriter.h
  2. +95 −1 framework/Source/iOS/GPUImageMovieWriter.m
@@ -51,6 +51,7 @@ extern NSString *const kGPUImageColorSwizzlingFragmentShaderString;
@property(nonatomic, assign, getter = isPaused) BOOL paused;
@property(nonatomic, copy) NSString *rawVideoPath;
// Initialization and teardown
- (id)initWithMovieURL:(NSURL *)newMovieURL size:(CGSize)newSize;
@@ -35,6 +35,10 @@ @interface GPUImageMovieWriter ()
BOOL isRecording;
}
@property(nonatomic, strong) id assetReader;
@property(nonatomic, strong) id assetReaderOutput;
// Movie recording
- (void)initializeMovieWithOutputSettings:(NSDictionary *)outputSettings compressVideoOutput:(BOOL)output;
@@ -271,9 +275,95 @@ - (void)startRecording;
if (audioInputReadyCallback == NULL)
{
[assetWriter startWriting];
}
});
// [assetWriter startSessionAtSourceTime:kCMTimeZero];
}
- (void)startMixAudio{
if(self.rawVideoPath){
//start reading
NSURL *audioURL = [NSURL fileURLWithPath:self.rawVideoPath];
NSURL *audioURL2 = [[NSBundle mainBundle] URLForResource:@"BOOMi_Greet" withExtension:@"mp4"];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil];
AVURLAsset* audioAsset2 = [[AVURLAsset alloc]initWithURL:audioURL2 options:nil];
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:kCMTimeZero error:nil];
AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
ofTrack:[[audioAsset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:kCMTimeZero error:nil];
// --- video reader
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:mixComposition error:nil];
// NSDictionary *audio_settings = [[NSDictionary alloc] initWithObjectsAndKeys:
// [NSNumber numberWithFloat:samplingRate],AVSampleRateKey,
// [NSNumber numberWithInt:numChannels],AVNumberOfChannelsKey,
// [NSNumber numberWithInt:32],AVLinearPCMBitDepthKey,
// [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
// [NSNumber numberWithBool:YES], AVLinearPCMIsFloatKey,
// [NSNumber numberWithBool:0], AVLinearPCMIsBigEndianKey,
// [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
// [NSData data], AVChannelLayoutKey,nil];
AVAssetReaderAudioMixOutput * assetReaderTrackOutput =
[[AVAssetReaderAudioMixOutput alloc] initWithAudioTracks:[mixComposition tracksWithMediaType:AVMediaTypeAudio]
audioSettings:nil];
// AVAssetTrack *assetTrack = [[mixComposition tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
// AVAssetReaderTrackOutput *assetReaderTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:assetTrack outputSettings:NULL];
[assetReader addOutput:assetReaderTrackOutput];
[assetReader startReading];
[assetWriterAudioInput requestMediaDataWhenReadyOnQueue:movieWritingQueue usingBlock:^
{
NSLog(@"Asset Writer ready :%d", assetWriterAudioInput.readyForMoreMediaData);
while (assetWriterAudioInput.readyForMoreMediaData & !audioEncodingIsFinished)
{
CMSampleBufferRef nextBuffer;
if([assetReader status] == AVAssetReaderStatusReading && (nextBuffer = [assetReaderTrackOutput copyNextSampleBuffer]))
{
if (nextBuffer)
{
NSLog(@"append buffer NextBuffer");
[assetWriterAudioInput appendSampleBuffer:nextBuffer];
}
}
else
{
NSLog(@"audio wrint done");
audioEncodingIsFinished = YES;
[assetWriterAudioInput markAsFinished];
}
}
}];
}
}
- (void)startRecordingInOrientation:(CGAffineTransform)orientationTransform;
@@ -363,6 +453,9 @@ - (void)finishRecordingWithCompletionHandler:(void (^)(void))handler;
- (void)processAudioBuffer:(CMSampleBufferRef)audioBuffer;
{
if(self.rawVideoPath){
return;
}
if (!isRecording)
{
return;
@@ -398,7 +491,7 @@ - (void)processAudioBuffer:(CMSampleBufferRef)audioBuffer;
}
previousAudioTime = currentSampleTime;
// NSLog(@"Recorded audio sample time: %lld, %d, %lld", currentSampleTime.value, currentSampleTime.timescale, currentSampleTime.epoch);
void(^write)() = ^() {
while( ! assetWriterAudioInput.readyForMoreMediaData && ! _encodingLiveVideo && ! audioEncodingIsFinished ) {
@@ -672,6 +765,7 @@ - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
[assetWriter startSessionAtSourceTime:frameTime];
startTime = frameTime;
[self startMixAudio];
});
}

0 comments on commit 94dd95a

Please sign in to comment.