Convert String to Float to move UISlider

261 views Asked by At

How I can convert time of my JSON data to float value. Below code is written to convert time to float and I passed it to UISlider as total length of audio. I want to move slider with that time spans

//Json Data
{
        duration = "00:03:45";
        id = 8;
}

//Audio player sider bar function

 if(audioController.playbackState == MPMusicPlaybackStatePlaying){


    if (isSelected == YES) {
        currentAutio = audioList[selectedAudio];
    } else {
        currentAutio = audioList[0];
    }
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;

    float value = [numberFormatter numberFromString:currentAutio.duration].floatValue;

    float currentPlaybackTime = [audioController currentPlaybackTime];
    float TotalLength = value;

    float remainingPlaybackTime = TotalLength - currentPlaybackTime;

    float sliderPosition = (currentPlaybackTime *100) / TotalLength;

    NSLog(@"current playbacktime %f",currentPlaybackTime);
    NSLog(@"TotalLength %f",TotalLength);
    NSLog(@"remainingPlaybackTime %f",remainingPlaybackTime);
    NSLog(@"sliderPosition %f",sliderPosition);

    //Update slider
    [progressSlider setValue:sliderPosition];

    //Update labels
    NSDate* d1 = [NSDate dateWithTimeIntervalSince1970:currentPlaybackTime];
    NSDate* d2 = [NSDate dateWithTimeIntervalSince1970:remainingPlaybackTime];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];

    NSString *currentTime = [dateFormatter stringFromDate:d1];
    NSString *ramainingTime = [dateFormatter stringFromDate:d2];

    [trackCurrentPlaybackTimeLabel setText:currentTime];
    [trackLengthLabel setText:[NSString stringWithFormat:@"-%@",ramainingTime]];

}

I am getting following output: Value = 0.00000

2

There are 2 answers

0
3li On

Just convert the duration from String to integer components. Then calculate the total duration in seconds.

You can get the integer components simply like this:

Swift

let timeComponents = duration.components(separatedBy: ":")

Objective C

NSArray *timeComponents = [duration componentsSeparatedByString:@":"];
  • timeComponents[0] will be the hours part.
  • timeComponents[1] will be the minutes part.
  • timeComponents[2] will be the seconds part.

Total seconds: (hours * 60 * 60) + (minutes * 60) + seconds.

After that, adjusting the slide bar value will be easy :)

0
Duncan C On

I don't think there is a built-in mechanism for converting a time interval into a float, but it's trivially easy to write:

func timeIntervalFrom(_ timeString: String) -> TimeInterval? {
    //Add code to validate that string is in correct format
    let components = time.components(separatedBy: ":")
    guard components.count == 3, 
        let hours = Double(components[0]), 
        let minutes = Double(components[1]),
        let seconds = Double(components[2]) else {
            return nil
    }
    return hours * 3600 +  minutes * 60 + seconds
}