Slider at the end of audio playback

In my project, I use AVAudioPlayer to play my saved sound. I am showing a slider for playing sound. The slider value is set using the current time property of the player. playSlider.value = avAudioPlayer.currentTime; This is a 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 the slider is set as follows

playSlider.maximumValue = audioPlayer.duration;

The problem I am facing is that the slider point will always return to the beginning of the sound after the game. This is mistake? How can i change this? Please provide your suggestion.

+4
source share
2 answers

I can’t say whether this is a mistake or not, but try using the AVAudioPlayer delegate AVAudioPlayer . In the didFinishPlaying method didFinishPlaying set the slider value to maximumValue. This may be one solution.

+1
source

Print the value of avAudioPlayer.currentTime before assigning it to playlider.value I think it will become null in the end. and he will assign a playlider.value zero

+1
source

Source: https://habr.com/ru/post/1380760/


All Articles