Sudheer Kumar Palchuri
Sudheer Kumar Palchuri

Reputation: 2954

How to show the progressbar while playing audio file in iOS

I have an audio file in the server, so I am able to play the audio file with the url. Below is the code. How can I show the progress bar while playing the audio file?

-(void)playselectedsong{
    AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
    [player play];
}

Upvotes: 1

Views: 3024

Answers (1)

Pradeep Singh
Pradeep Singh

Reputation: 762

Create an AVAsset with the URL and then fetch the duration of the AVAsset created with the URL.

AVAsset *audio = [AVAsset assetWithURL:]; // create an audio instance.
float duration = CMTimeGetSeconds([audio currentTime]); // gives the seconds of audio file.

AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: audio]; // create a player item.

AVPlayer *avPlayer = [[AVPlayer alloc] initWithPlayerItem: item]; // initialise the AVPlayer with the AVPlayerItem.

[avPlayer play]; // play the AVPlayer

Then you can design progress bar which will update every seconds after playing the audio file.

UIProgressView *myProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];

invoke an update method after every second.

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(methodToUpdateProgress) userInfo:nil repeats:YES];

Then in "methodToUpdateProgress" update the progress bar.

[myProgressView setProgress:someFloat animated:YES];

Upvotes: 5

Related Questions