Search code examples
iphoneiosaudioavaudioplayeruislider

Slider stop at the end of playing audio


In my project I am using an AVAudioPlayer to play my saved audio.I am showing the slider for playing audio. The value of the slider is set using the current time property of the player. playSlider.value = avAudioPlayer.currentTime; This is the timer code

updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:p repeats:YES];



//Calls the update current time function 
- (void)updateCurrentTime
{
    [self updateCurrentTimeForPlayer:self.player];
}


//Updates the current time while the audio is playing
-(void)updateCurrentTimeForPlayer:(AVAudioPlayer *)avAudioPlayer
{
    currentTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)avAudioPlayer.currentTime / 60, (int)avAudioPlayer.currentTime % 60, nil];
    playSlider.value = avAudioPlayer.currentTime;

    if (playSlider.value==playSlider.minimumValue) 
    {
        moveToBeginningButton.enabled = NO;
        rewindButton.enabled = NO;
        NSLog(@"PlaySliderMaxValue1 === %f",playSlider.maximumValue);
        NSLog(@"PlaySliderValue1 === %f",playSlider.value);
    }
    else  
    {  
        moveToBeginningButton.enabled = YES;
        rewindButton.enabled = YES; 
        NSLog(@"PlaySliderMaxValue2 === %f",playSlider.maximumValue);
        NSLog(@"PlaySliderValue2 === %f",playSlider.value);

    }
}

The maximum value of slider is set as follows

playSlider.maximumValue = audioPlayer.duration;

The problem that I am experiencing is that the slider point will always return to the beginning of the audio after playing.Is this a bug? How can I change this? Please provide your suggestion.


Solution

  • I cannot say whether this is a bug or not, But try using delegate methods of AVAudioPlayer. In didFinishPlaying method set Slider value to maximumValue. This could be one of the solution.