[SOLVED + CODE] SeekSlider range problem


#1

Hi guys,

I am trying to make a slider seek the position of the song that is being played from the beginning to the end of the song.

The problem is that when i try to set the minimum, maximum and the interval of the slider, the thumb of the slider appears in the middle of the track and doesn't move.

Also I have a timerCallback() function where I have implemented this :

slider.setvalue(transportSource.getCurrentPosition(), dontSendNotification);

So, when i try to compile the code without the range of the slider, the song starts playing and the thumb of the slider mover gradually from the beginning to the end of the slider track. The timer is set to startTimer(20); , so the thumb moves with the speed 0.02 seconds.

Now, when i try to set the range of the slider to slider.setRange( 0.0, transportSource.getLengthInSeconds(), 1.0); , the thumb of the slider appears in the middle of the slider track and it doesn't move.

When I set the range slider.setRange( 0.0, 100.0, transportSource.getLengthInSeconds() / 100.0 ); , the slider starts from the beginning to the end as it should but it only has the interval of 100.0 and not the total length in seconds of the song.

Has anyone had this problem before, and can you tell me how to solve this ?

Logically, the slider should be set like this : 

slider.setRange( 0.0 , transportSource.getLengthInSeconds(), transportSource.getLengthInSeconds()/transportSource.getLengthInSeconds());

but when I try to compile this it the program will not run.

Maybe I'm doing everything wrong, but this is how i thought it should be.

Thank you.

 

 

Edit :

So guys, I had this problem and I solved it by myself. Maybe, someone needs this solution and I am going to share it. ;)

What you need to make a slider seek the current position of the song from the beginning to the end is that you have to inherit the Timer class and use the method timerCallback.

After creating the slider you need to set range of it from 0 to the total length of the song in seconds.

You will need to set the interval too, so in the interval you will have to divide the totalLength by itself ( totalLength / totalLength ) which will be equal to 1 second. Now the the thumb of the slider will seek the track of the slider by one seconds till it reaches the end of the song that is being played. 

That's all you need to make the slider seek the song from the start to the end of it.

Below you have the code :

First you need to call the startTimer() function and pass the value you like. For ex. in this case I set it to startTimer(20); , so the thumb of the slider with be repainted faster and there won't be a delay.

Don't forget to put the startTimer(20) inside the constructor. Also, the currentPosition is declared in the class as : double currentPosition;

, and in he constructor : cunrrentPosition = transportSource.getCurrentPosition();


void MainContentComponent::timerCallback()
{
    
    const RelativeTime position(musicList.transportSource.getCurrentPosition());
    const int minutes = ((int)position.inMinutes()) % 60;
    const int seconds = ((int)position.inSeconds()) % 60;
    const int millis = ((int)position.inMilliseconds()) % 1000;
    const String positionString(String::formatted("%02d:%02d", minutes, seconds));
    
    if (musicList.transportSource.isPlaying())
    {
        
        /*This sets the duration info of the song to the label*/
        durationInfo.setText(positionString, dontSendNotification);
    
        /*Now we need to convert the transportSource.getLengthInSeconds() to second units for the slider*/

        const RelativeTime mLength(transportSource.getLengthInSeconds());
        
        const int totalLengthInSeconds = ((int)mLength.inSeconds());
        
        double totalLength;
        for (currentPosition = 0; currentPosition < totalLengthInSeconds; currentPosition++)
        {
            if (currentPosition = totalLengthInSeconds)
            {
                totalLength = currentPosition;
            }
        }
        musicDuration.setRange(0.0, totalLength, totalLength/ totalLength);
        /*This will make the slider seek the current position each second till the end of the song.
        musicDuration.setValue(transportSource.getCurrentPosition(), dontSendNotification);
        
    }
    else if (musicList.transportSource.hasStreamFinished())
    {
        durationInfo.setText("Finished", dontSendNotification);
    }
    else
    {
        durationInfo.setText(positionString, dontSendNotification);
    }
}

If you want to change the position of the song by cliking on the slider you will have to implement this method and also you will have to use a Slider::Listener :

sliderValueChange(Slider* slider);

sliderDragEnded(Slider* slider);


void MainContentComponent::sliderValueChanged(Slider *slider)
{
    (&musicDuration == slider)
    {
        transportSource.setPosition(musicDuration.getValue());
        
    }
}


/*We implement this method in order to set the position of the song by dragging the slider*/
void MainContentComponent::sliderDragEnded(Slider * slider)
{
    if (slider == &musicDuration)
    {
        transportSource.setPosition(musicDuration.getValue());
        
    }
    
}

That's all guys. cool


#2

Someone should really update the forum’s old posts that have a ton of '&nbsp;'s in them

void MainContentComponent::timerCallback()
{

    const RelativeTime position(musicList.transportSource.getCurrentPosition());
    const int minutes = ((int)position.inMinutes()) % 60;
    const int seconds = ((int)position.inSeconds()) % 60;
    const int millis = ((int)position.inMilliseconds()) % 1000;
    const String positionString(String::formatted("%02d:%02d", minutes, seconds));
   
    if (musicList.transportSource.isPlaying())
    {
   
    /*This sets the duration info of the song to the label*/
        durationInfo.setText(positionString, dontSendNotification);
    
    /*Now we need to convert the transportSource.getLengthInSeconds() to second units for the slider*/
        const RelativeTime mLength(transportSource.getLengthInSeconds());
        const int totalLengthInSeconds = ((int)mLength.inSeconds());
        double totalLength;
        for (currentPosition = 0; currentPosition &lt; totalLengthInSeconds; currentPosition++)
        {
            if (currentPosition = totalLengthInSeconds)
            {
                totalLength = currentPosition;
            }
        }
        musicDuration.setRange(0.0, totalLength, totalLength/ totalLength);
        /*This will make the slider seek the current position each second till the end of the song.
        musicDuration.setValue(transportSource.getCurrentPosition(), dontSendNotification);

    }
    else if (musicList.transportSource.hasStreamFinished())
    {
        durationInfo.setText("Finished", dontSendNotification);
    }
    else
    {
        durationInfo.setText(positionString, dontSendNotification);
    }
}

If you want to change the position of the song by cliking on the slider you will have to implement this method and also you will have to use a Slider::Listener :sliderValueChange(Slider* slider);sliderDragEnded(Slider* slider);

void MainContentComponent::sliderValueChanged(Slider *slider)
{
    if( musicDuration == slider)
    {
        transportSource.setPosition(musicDuration.getValue());
    }
}


/*We implement this method in order to set the position of the song by dragging the slider*/
void MainContentComponent::sliderDragEnded(Slider * slider)
{
    if (slider == &amp;musicDuration)
    {
        transportSource.setPosition(musicDuration.getValue());
    }
}