2013-05-13 63 views
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天开始搜索这个。但没有运气:(

回答

3
-(void)ViewDidLoad{ 

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"KARAOKE neele neele ambar pe Karaoke With Lyrics.mp4" ofType:nil]; 
NSURL *url = [NSURL fileURLWithPath:urlStr]; 
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
[self.view addSubview:moviePlayer.view]; 
moviePlayer.view.frame = CGRectMake(10, 10, 200, 200); 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playbackStateChanged) 
              name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; 


NSArray *pathComponents = [NSArray arrayWithObjects: 
          [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], 
          @"MyAudioMemo.m4a", 
          nil]; 
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents]; 

// Setup audio session 
AVAudioSession *session = [AVAudioSession sharedInstance]; 
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

// Define the recorder setting 
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; 

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; 
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]; 

// Initiate and prepare the recorder 
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL]; 
recorder.delegate = self; 
recorder.meteringEnabled = YES; 
[recorder prepareToRecord]; 

}

-(IBAction)ButtonPlayClicked:(id)sender{ 
if (player.playing) { 
    [player stop]; 
} 

[moviePlayer play]; 
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil]; 

if (!recorder.recording) { 
    AVAudioSession *session = [AVAudioSession sharedInstance]; 
    [session setActive:YES error:nil]; 

    // Start recording 
    [recorder record];   
} 

}

-(IBAction)ButtonStopClicked:(id)sender{ 
[moviePlayer stop]; 
[recorder stop]; 
if(player) 
[player stop]; 
} 

- (void) playbackStateChanged { 
MPMoviePlaybackState playbackState = moviePlayer.playbackState; 
if(playbackState == MPMoviePlaybackStateStopped) { 
    NSLog(@"MPMoviePlaybackStateStopped"); 
    [recorder stop]; 

    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setActive:NO error:nil]; 

} else if(playbackState == MPMoviePlaybackStatePlaying) { 
    NSLog(@"MPMoviePlaybackStatePlaying"); 
    [recorder record]; 
} else if(playbackState == MPMoviePlaybackStatePaused) { 
    NSLog(@"MPMoviePlaybackStatePaused"); 
    [recorder pause]; 
} else if(playbackState == MPMoviePlaybackStateInterrupted) { 
    NSLog(@"MPMoviePlaybackStateInterrupted"); 
} else if(playbackState == MPMoviePlaybackStateSeekingForward) { 
    NSLog(@"MPMoviePlaybackStateSeekingForward"); 
} else if(playbackState == MPMoviePlaybackStateSeekingBackward) { 
    NSLog(@"MPMoviePlaybackStateSeekingBackward"); 
} 
} 

-(IBAction)ButtonRecordPlayClicked:(id)sender{ 
NSLog(@"%@",recorder.url); 

if (!recorder.recording){ 
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil]; 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil]; 
    [player setDelegate:self]; 
    [player setVolume:10.0]; 
    [player play]; 
} 

MPMediaQuery *everything = [[MPMediaQuery alloc] init]; 
NSMutableArray *array_mp3 = [[NSMutableArray alloc] init]; 
NSArray *itemsFromGenericQuery = [everything items]; 

for (MPMediaItem *song in itemsFromGenericQuery) 
{ 
    NSString *title = [NSString stringWithFormat:@"%@",[song valueForProperty:MPMediaItemPropertyAlbumTitle]]; 
    if(title) 
    { 
     if(![array_mp3 containsObject:title]) 
      [array_mp3 addObject:title]; 
    } 
} 
MPMediaItem *nowPlayingMediaItem = [[MPMusicPlayerController iPodMusicPlayer] nowPlayingItem]; 
NSURL* songURL = [nowPlayingMediaItem valueForProperty:MPMediaItemPropertyAssetURL]; 
AVAsset* songAsset = [AVURLAsset URLAssetWithURL:songURL options:nil]; 
NSString* lyrics = [songAsset lyrics]; 
NSLog(@"%@",lyrics); 
} 

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Done" message: @"Finish playing the recording!"delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[alert show]; 
} 

它认为它-111帮助你。

+1

是的,这工作,但显然它也记录音频从耳机插入时的视频,而不仅仅是麦克风录音。 – ScarletWitch 2013-05-14 07:40:54

+0

感谢队友..你已经解决了我的情况,花了至少2天ays很糟糕..我希望你能多点赞扬。 – 2013-08-03 07:26:44