2013-04-09 117 views
0

我正在创建一个应用程序,用户可以在其中录制和播放录制的文件。 我可以录制和播放,但是我面临的问题是,在播放(聆听)录制文件时,如果用户按下录制按钮,它将开始录制,并且按下停止位置以恢复之前录制的位置。使用AVAudioRecorder和AVAudioPlayer录制和播放

我想要的是当用户正在听录音时,在他按下录音按钮的同时,最后一个录音(他正在播放)应该被销毁。

总之,我不想恢复最后的录音,我想播放最近的录音。

这是我的录音和播放代码。

-(void)showStopbutton 
    { 
     if (flag_for_mic_button == 0) 
     { 
      // All in vain the following condition not making any effect palyer is resuming back 
      if ([audioPlayer isPlaying]) 
      { 
       [audioPlayer stop]; 
       audioPlayer = nil; 
       audioPlayer = NO; 
      } 
      // 

      [btn_Mic setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal]; 
      lbl_Press.hidden = TRUE; 
      lbl_updateTime.hidden = FALSE; 
      [self startRecording]; 
      flag_for_mic_button = 1; 
     } 
     else if (flag_for_mic_button ==1) 
     { 
      [self stopRecording]; 

      // All in vain the following condition not making any effect palyer is resuming back 

      if ([audioPlayer isPlaying]) 
      { 
       audioPlayer = NO; 
       [audioPlayer stop]; 
       audioPlayer = nil; 
      } 

      // 

      [btn_Mic setImage:[UIImage imageNamed:@"mic.png"] forState:UIControlStateNormal]; 
      lbl_Press.hidden = FALSE; 
      lbl_updateTime.hidden = TRUE; 
      flag_for_mic_button = 0; 
     } 
    } 

-(void)startRecording 
{ 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    NSError *err = nil; 
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
    AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride); 
    if(err){ 
     NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); 
     return; 
    } 
    [audioSession setActive:YES error:&err]; 
    err = nil; 
    if(err){ 
     NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); 
     return; 
    } 
    recordSetting = [[NSMutableDictionary alloc]init]; 
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; 
    [recordSetting setValue:[NSNumber numberWithInt:44100.0f] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 
    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
    [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 
    [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; 
    NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0]; 
    caldate =[now description]; 
    recorderFilePath = [NSString stringWithFormat:@"%@/%@ MyRecording.caf",DOCUMENTS_FOLDER,caldate]; 
    NSURL *url = [NSURL fileURLWithPath:recorderFilePath]; 
    err=nil; 
    audioRecorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&err]; 
    [audioRecorder setDelegate:self]; 
    [audioRecorder prepareToRecord]; 
    audioRecorder.meteringEnabled = YES; 

    [audioRecorder record]; 

    [self setTimer]; 
    [self hidePlaybackView]; 
} 

-(void)stopRecording 
{ 
    [audioRecorder stop]; 
    [start_Timer invalidate]; 
    [self showPlaybackView]; 

} 

-(void)playSound 
{ 
// [self playBackTimer]; 

    if (!recorderFilePath) 
    { 
     recorderFilePath = [NSString stringWithFormat:@"%@/%@ MyRecording.caf",DOCUMENTS_FOLDER,caldate]; 
    } 

    if(soundID) 
    { 
     AudioServicesDisposeSystemSoundID(soundID); 
    } 


    //Get a URL for the sound file 
    NSURL *filePath = [NSURL fileURLWithPath:recorderFilePath isDirectory:NO]; 

    //Use audio sevices to create the sound 
    AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain(filePath), &soundID); 

    //Use audio services to play the sound 
    AudioServicesPlaySystemSound(soundID); 

    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:filePath error:nil]; 

    [audioPlayer play]; 

    audioPlayer.delegate = self; 

} 

编辑:

-(void)showPlaybackView 
{ 
    view_playback = [[UIView alloc]initWithFrame:CGRectMake(0, 600, 320, 80)]; 
    view_playback.backgroundColor = [UIColor grayColor]; 
    [self.view addSubview:view_playback]; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.4]; 
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; 
    view_playback.frame = CGRectMake(0, 325, 320, 55); 
    [UIView commitAnimations]; 

    btn_Playback = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn_Playback setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; 
    btn_Playback.frame = CGRectMake(10, 10, 35, 35); 
    [view_playback addSubview:btn_Playback]; 
    [btn_Playback addTarget:self action:@selector(playSound) forControlEvents:UIControlEventTouchUpInside]; 

    btn_done = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btn_done setTitle:@"Done" forState:UIControlStateNormal]; 
    btn_done.frame = CGRectMake(265, 15, 45, 30); 
    btn_done.titleLabel.font = [UIFont systemFontOfSize:14.0f]; 
    [view_playback addSubview:btn_done]; 

    [view_playback addSubview:playback_slider]; 


    [btn_done addTarget:self action:@selector(Done) forControlEvents:UIControlEventTouchUpInside]; 
} 

任何帮助将是明显的。

感谢

+0

在' - (void)stopRecording'方法中,您正在停止录制而不是删除现有的录制文件。 Y? – 2013-04-09 10:06:30

+0

如果我删除该录音然后如何听录制的文件 – 2013-04-09 10:08:30

+0

感谢您的帮助,但我有一个按钮来播放录制的文件,但同时我给规定的记录一个新的文件,并在录制后按下播放按钮将播放最近的录音。 此外,使用'[audioPlayer stop]'不起作用,它只是暂停当前正在播放的文件。 – 2013-04-09 10:19:41

回答

0

我停止音频播放器和不让它通过使用下面的代码恢复回来。

if([audioPlayer isPlaying]) 
{ 
    audioPlayer.currentTime=0; 
} 

快乐编码!

0

试试这个办法:

-(void)showStopbutton 
{ 
     if (![audioPlayer isPlaying]) 
     { 
      [audioPlayer start]; 
      // other stuff 
     } 

     if ([audioPlayer isPlaying]) 
     { 
      [audioPlayer stop]; 
      // Delete the previous recording in the delegate method of audio player  
     } 

} 
相关问题