2
我正試圖在屏幕的一個小區域播放視頻,並同時錄製用戶的歌聲。但是,它似乎記錄不成功。 我已經添加了一些代碼給你,指出可能的失誤。不能同時播放MPMoviePlayerController和通過AVAudioRecorder錄製?
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMedium],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM],
AVFormatIDKey,
nil];
NSError *error = nil;
[audioRecorder setDelegate:self];
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
[self initialiseVideo];
}
- (void) initialiseVideo
{
[audioRecorder record];
NSString *videoName = @"Medium";
NSString *path = [[NSBundle mainBundle] pathForResource:videoName ofType:@"mp4"];
if (path) {
NSURL *url = [NSURL fileURLWithPath:path] ;
moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.scalingMode = MPMovieScalingModeFill;
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setFullscreen:NO];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[moviePlayer view] setFrame:CGRectMake(58, 166, 197, 124)];
}
[moviePlayer play];
}
#pragma mark -
#pragma mark MPMoviePlayerController Delegate
#pragma mark -
- (void) moviePlayBackDidFinish:(NSNotification*)aNotification
{
[submitButton setUserInteractionEnabled:YES];
[audioRecorder stop] ;
[moviePlayer.view removeFromSuperview];
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
{
NSLog (@"audioRecorderDidFinishRecording:successfully:");
NSLog(@"%@", aRecorder.url);
NSLog(@"%@", aRecorder.description);
// your actions here
}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder
error:(NSError *)error
{
NSLog(@"Encode Error occurred");
}
-(IBAction)playAudio:(id)sender
{
if (!audioPlayer.playing)
{
if (audioPlayer)
[audioPlayer release];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:audioRecorder.url
error:&error];
audioPlayer.delegate = self;
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[audioPlayer play];
}
else
{
}
}
#pragma mark -AV delegate methods
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(@"Decode Error occurred");
}
我希望這是有道理的。流動似乎並不即使
[audioRecorder停止]進入
audioRecorderDidFinishRecording
;
請大家幫忙。我從過去3天開始搜索這個。但沒有運氣:(
是的,這工作,但顯然它也記錄音頻從耳機插入時的視頻,而不僅僅是麥克風錄音。 – ScarletWitch 2013-05-14 07:40:54
感謝隊友..你已經解決了我的情況,花了至少2天ays很糟糕..我希望你能多點讚揚。 – 2013-08-03 07:26:44