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

次のコードを使用して、Audio Player を停止し、再開させません。

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

ハッピーコーディング!

于 2013-04-11T11:13:07.550 に答える
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 に答える