Question

I've been searching for a simple way to show the progress of playing an MP3 in a UIProgressView. I have a feeling it's going to involve an NSTimer object but I'm not sure how to implement it.

Here's what I have so far.

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UISlider *VolumeLevel;
@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;

@end

@implementation ViewController

AVAudioPlayer *player;


- (IBAction)play:(id)sender {

    [player play];

}

- (IBAction)pause:(id)sender {

    [player stop];

}
- (IBAction)volume:(id)sender {

    player.volume = _VolumeLevel.value;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *songURL =[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Gregory Is Here" ofType:@"mp3"]];
    player =[[AVAudioPlayer alloc]initWithContentsOfURL:songURL error:nil];
    player.volume = 0.5;


    // Do any additional setup after loading the view, typically from a nib.
}
Was it helpful?

Solution

Using a NSTImer, you can do :

- (IBAction)play:(id)sender {
    [player play];
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
}

- (IBAction)pause:(id)sender {
    [player stop];
    [myTimer invalidate];
}

- (void)updateProgressBar {
    float progress = player.currentTime / player.duration;
    [myProgressBar setProgress:progress animated:YES];
}

You can change the timer timeInterval parameter to update the progress bar more or less frequently (1.0 is for 1 second).

OTHER TIPS

 -(void) setPlayProgress
        {
            if (player.isPlaying) {
                slider.value=abs(player.currentTime);
                NSString *duration=[self getTimeStrFromInterval:player.duration-player.currentTime];
                lblDuration.text=duration;
            }
            [self performSelector:@selector(setPlayProgress) withObject:nil afterDelay:0.1];

        }

// Show time

    -(NSString *) getTimeStrFromInterval:(NSInteger) timeSinceStart{

    int seconds = timeSinceStart;
    int minutes = seconds/60;
    int hours = minutes/60;
    minutes = minutes%60;
    seconds = seconds%60;
    NSString *strHours;
    NSString *strMinutes;
    NSString *strSeconds;
    strHours = [NSString stringWithFormat:@"%d",hours];
    strMinutes = [NSString stringWithFormat:@"%d",minutes];
    strSeconds = [NSString stringWithFormat:@"%d",seconds];
    if (hours<10) {
        strHours = [NSString stringWithFormat:@"0%d",hours];
    }
    if (minutes<10) {
        strMinutes = [NSString stringWithFormat:@"0%d",minutes];
    }
    if (seconds<10) {
        strSeconds = [NSString stringWithFormat:@"0%d",seconds];
    }
    NSString* intervalString;
    if ([strHours intValue]>0) {
         intervalString = [NSString stringWithFormat:@"%@:%@:%@",strHours,strMinutes,strSeconds];
    }
    else{
         intervalString = [NSString stringWithFormat:@"%@:%@",strMinutes,strSeconds];
    }


    return intervalString;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top