0

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

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

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

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

   -(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];
}

任何帮助都是不言而喻的。

谢谢

4

2 回答 2

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       
        }

}
于 2013-04-09T10:13:58.803 回答
0

我停止了音频播放器,并且使用以下代码不要让它恢复。

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

快乐编码!

于 2013-04-11T11:13:07.550 回答