I am implementing an audio player. It works fine but the slider drag does not update the song. The song starts from the start instead of being fast forward.
/*
* Updates the time label display and
* the current value of the slider
* while audio is playing
*/
- (void)updateTime:(NSTimer *)timer {
//to don't update every second. When scrubber is mouseDown the the slider will not set
NSLog(#"slider = %f",self.currentTimeSlider.value);
long currentPlaybackTime = [self.audioPlayer getCurrentAudioTime];
// self.currentTimeSlider.value = currentPlaybackTime / [self.audioPlayer getAudioDuration];
if ((currentPlaybackTime / [self.audioPlayer getAudioDuration]) > self.currentTimeSlider.value )
{
self.currentTimeSlider.value = currentPlaybackTime / [self.audioPlayer getAudioDuration];
self.timeElapsed.text = [NSString stringWithFormat:#"%#",
[self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];
self.duration.text = [NSString stringWithFormat:#"%#",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}
else{
//self.currentTimeSlider.value = currentPlaybackTime / [self.audioPlayer getAudioDuration];
NSLog(#"sliderValuessszz = %f",self.currentTimeSlider.value);
NSLog(#"audio time = %f",[self.audioPlayer getCurrentAudioTime]);
self.timeElapsed.text = [NSString stringWithFormat:#"%#",
[self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];
NSLog(#"time elapsed = %d",self.timeElapsed.text);
self.duration.text = [NSString stringWithFormat:#"%#",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}
}
- (IBAction)sliderChanged:(id)currentTimeSlider
{
NSLog(#"sliderValue = %f",self.currentTimeSlider.value);
NSLog(#"sliderValuesss = %f",self.currentTimeSlider.value);
//[self.timer invalidate];
[self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];
[NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:NO];
/*if (!self.isPaused){
[self.audioPlayer stopAudio];
[self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];
[self.audioPlayer prepareToPlayAudio];
[self.audioPlayer playAudio];
}
else
{
[self.audioPlayer setCurrentAudioTime: self.currentTimeSlider.value];
}
*/
}
On slider drag the song starts from beginning and timer label also shows the start time. Can someone please help me solve this.
Thanks,
Your MyAudioPlayer.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface MyAudioPlayer : UIViewController
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
// Public methods
- (void)initPlayer:(NSString*) audioFile fileExtension:(NSString*)fileExtension;
- (void)playAudio;
- (void)pauseAudio;
- (void)setCurrentAudioTime:(float)value;
- (float)getAudioDuration;
- (NSString*)timeFormat:(float)value;
- (NSTimeInterval)getCurrentAudioTime;
#end
the corresponding .m file
#implementation MyAudioPlayer
/*
* Init the Player with Filename and FileExtension
*/
- (void)initPlayer:(NSString*) audioFile fileExtension:(NSString*)fileExtension
{
NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:audioFile withExtension:fileExtension];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
}
/*
* Simply fire the play Event
*/
- (void)playAudio {
[self.audioPlayer play];
}
/*
* Simply fire the pause Event
*/
- (void)pauseAudio {
[self.audioPlayer pause];
}
/*
* Format the float time values like duration
* to format with minutes and seconds
*/
-(NSString*)timeFormat:(float)value{
float minutes = floor(lroundf(value)/60);
float seconds = lroundf(value) - (minutes * 60);
int roundedSeconds = lroundf(seconds);
int roundedMinutes = lroundf(minutes);
NSString *time = [[NSString alloc]
initWithFormat:#"%d:%02d",
roundedMinutes, roundedSeconds];
return time;
}
/*
* To set the current Position of the
* playing audio File
*/
- (void)setCurrentAudioTime:(float)value {
[self.audioPlayer setCurrentTime:value];
}
/*
* Get the time where audio is playing right now
*/
- (NSTimeInterval)getCurrentAudioTime {
return [self.audioPlayer currentTime];
}
/*
* Get the whole length of the audio file
*/
- (float)getAudioDuration {
return [self.audioPlayer duration];
}
#end
the init method:
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
the view controller class
#import "MyAudioPlayer.h"
#interface ViewController : UIViewController
#property (nonatomic, strong) YMCAudioPlayer *audioPlayer;
#property (weak, nonatomic) IBOutlet UISlider *currentTimeSlider;
#property (weak, nonatomic) IBOutlet UIButton *playButton;
#property (weak, nonatomic) IBOutlet UILabel *duration;
#property (weak, nonatomic) IBOutlet UILabel *timeElapsed;
#property BOOL isPaused;
#property BOOL scrubbing;
#property NSTimer *timer;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.audioPlayer = [[MyAudioPlayer alloc] init];
[self setupAudioPlayer:#"audiofile"];
}
/*
* Setup the AudioPlayer with
* Filename and FileExtension like mp3
* Loading audioFile and sets the time Labels
*/
- (void)setupAudioPlayer:(NSString*)fileName
{
//insert Filename & FileExtension
NSString *fileExtension = #"mp3";
//init the Player to get file properties to set the time labels
[self.audioPlayer initPlayer:fileName fileExtension:fileExtension];
self.currentTimeSlider.maximumValue = [self.audioPlayer getAudioDuration];
//init the current timedisplay and the labels. if a current time was stored
//for this player then take it and update the time display
self.timeElapsed.text = #"0:00";
self.duration.text = [NSString stringWithFormat:#"-%#",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration]]];
}
/*
* PlayButton is pressed
* plays or pauses the audio and sets
* the play/pause Text of the Button
*/
- (IBAction)playAudioPressed:(id)playButton
{
[self.timer invalidate];
//play audio for the first time or if pause was pressed
if (!self.isPaused) {
[self.playButton setBackgroundImage:[UIImage imageNamed:#"audioplayer_pause.png"]
forState:UIControlStateNormal];
//start a timer to update the time label display
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:YES];
[self.audioPlayer playAudio];
self.isPaused = TRUE;
} else {
//player is paused and Button is pressed again
[self.playButton setBackgroundImage:[UIImage imageNamed:#"audioplayer_play.png"]
forState:UIControlStateNormal];
[self.audioPlayer pauseAudio];
self.isPaused = FALSE;
}
}
/*
* Updates the time label display and
* the current value of the slider
* while audio is playing
*/
- (void)updateTime:(NSTimer *)timer {
//to don't update every second. When scrubber is mouseDown the the slider will not set
if (!self.scrubbing) {
self.currentTimeSlider.value = [self.audioPlayer getCurrentAudioTime];
}
self.timeElapsed.text = [NSString stringWithFormat:#"%#",
[self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];
self.duration.text = [NSString stringWithFormat:#"-%#",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}
/*
* Sets the current value of the slider/scrubber
* to the audio file when slider/scrubber is used
*/
- (IBAction)setCurrentTime:(id)scrubber {
//if scrubbing update the timestate, call updateTime faster not to wait a second and dont repeat it
[NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:NO];
[self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];
self.scrubbing = FALSE;
}
/*
* Sets if the user is scrubbing right now
* to avoid slider update while dragging the slider
*/
- (IBAction)userIsScrubbing:(id)sender {
self.scrubbing = TRUE;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
self.audioPlayer = [[MyAudioPlayer alloc] init];
setUpAudioPlayer
self.timeElapsed.text = #"0:00";
self.duration.text = [NSString stringWithFormat:#"-%#",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration]]];
UpdateTime
- (void)updateTime:(NSTimer *)timer {
//to don't update every second. When scrubber is mouseDown the the slider will not set
if (!self.scrubbing) {
self.currentTimeSlider.value = [self.audioPlayer getCurrentAudioTime];
}
self.timeElapsed.text = [NSString stringWithFormat:#"%#",
[self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];
self.duration.text = [NSString stringWithFormat:#"-%#",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}
Reference
Related
The app is a music player, and I need to show a trackTimeLabel that updates the playback duration during the song.
So, update the label every second with the playback track time duration.
Right now I'm doing a hack where I am just counting seconds that have nothing to do with the song, but that's not a good solution:
- (void)viewWillAppear:(BOOL)animated {
currentItem = [musicPlayer nowPlayingItem];
_titleLabel.text = [self currentItemValue:MPMediaItemPropertyTitle];
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(timerDidTick:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void) timerDidTick:(NSTimer*) theTimer{
long currentPlaybackTime = musicPlayer.currentPlaybackTime;
int currentHours = (int)(currentPlaybackTime / 3600);
int currentMinutes = (int)((currentPlaybackTime / 60) - currentHours*60); // Whole minutes
int currentSeconds = (currentPlaybackTime % 60);
_trackTimeLabel.text = [NSString stringWithFormat:#"%i:%02d:%02d", currentHours, currentMinutes, currentSeconds];
}
Apple has a MPMediaItem class which I can get a MPMediaItemPropertyPlaybackDuration for the track, but I can't seem to get anything working with that.
Step 1
In your ViewController.h
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
{
AVAudioPlayer * player;
}
#property (weak, nonatomic) IBOutlet UISlider *seekSlider;
#property (weak, nonatomic) IBOutlet UILabel *lbl;
Step 2
In your ViewController.m
- (void)viewDidLoad {
NSURL * fileURL = [[NSBundle mainBundle] URLForResource:#"01 - Saturday Saturday - DownloadMing.SE" withExtension:#"mp3"];
NSError * error = nil;
player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:&error];
if(error)
NSLog(#"Error : %# ", error);
[player prepareToPlay];
player.volume = 0.5;
self.seekSlider.maximumValue = player.duration;
}
-(void)timerMethod:(NSTimer *)timer
{
float progress = player.currentTime;
// if(!self.seekSlider.isFocused)
self.seekSlider.value = progress;
_lbl.text = [NSString stringWithFormat:#"%.f:%.2d", (progress / 60), ((int)progress % 60 )];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)play_pause_clicked:(id)sender {
if(player.playing)
{
[player pause];
[(UIButton*)sender setTitle:#"Play" forState:UIControlStateNormal];
}
else
{
[player play];
[(UIButton*)sender setTitle:#"Pause" forState:UIControlStateNormal];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerMethod:) userInfo:nil repeats:YES];
}
}
- (IBAction)seekPlayer:(id)sender {
player.currentTime = [(UISlider*)sender value];
}
- (IBAction)changeVolume:(id)sender {
player.volume = [(UISlider*)sender value];
}
This is perfect working code for me... Try this, I hope, this will help you :)
I have a viewController that streams audio from the web. When I leave that page of my app, the audio keeps playing (this is good). However, when I go back, the viewdidload method creates a second audioplayer. I can do this over and over until I have so many AVplayers. It sounds incredibly echo-y. Here is all my relevant code.
#import "ListenViewController.h"
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVPlayerItem.h>
#import <AVFoundation/AVPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "RSPlayPauseButton.h"
#interface ListenViewController () <AVAudioPlayerDelegate, AVAudioSessionDelegate>
#property (nonatomic, strong) AVPlayer *player;
#property (nonatomic, strong) AVPlayerItem *playerItem;
#property (nonatomic, strong) RSPlayPauseButton *playPauseButton;
#property (nonatomic, copy) NSDate *now;
#end
#implementation ListenViewController
static int min = -2;
-(void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
//[self.player pause];
min = -2;
}
-(void) viewDidLoad
{
[super viewDidLoad];
/*
if ([AVAudioSession sharedInstance].isInputAvailable) {
NSLog(#"XXXXXXXXX");
}
*/
NSURL *streamingURL = [NSURL URLWithString:#"http://URL.m3u"];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:streamingURL];
self.playerItem = playerItem;
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
[player setAllowsExternalPlayback:NO];
CMTime tm = CMTimeMake(1, 1);
[player addPeriodicTimeObserverForInterval:tm
queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
/*
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss:mmm"];
NSLog(#"%#",[DateFormatter stringFromDate:[NSDate date]]);
*/
if (!self.now) {
self.now = [NSDate date];
}
/*
NSDate *now = [NSDate date];
NSTimeInterval interval = [now timeIntervalSinceDate:self.now];
self.interval += 0.01;
interval -= (10 * self.rw2);
interval -= (60 * self.rw3);
interval += (10 * self.ff2);
self.timeLabel.text = [NSString stringWithFormat:#"%f", interval];
NSLog(#"INTERVAL: %#", [NSString stringWithFormat:#"%f", self.interval]);
*/
if (CMTimeGetSeconds(self.player.currentTime) >= 0) {
self.timeLabel.text = [NSString stringWithFormat:#"%.0f", CMTimeGetSeconds(self.player.currentTime)];
NSTimeInterval time = CMTimeGetSeconds(self.player.currentTime);
//int min = time/60;
int sec = lroundf(time) % 60;
if (sec == 0) {
++min;
}
NSLog(#"sec: %d", sec);
// update your UI with timeLeft
self. timeLabel.text = [NSString stringWithFormat:#"%.2d:%.2d", min,sec];
}
}];
[self setPlayer:player];
[player play];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if (!self.playPauseButton) {
self.playPauseButton = [[RSPlayPauseButton alloc] initWithFrame:self.view.frame];
self.playPauseButton.tintColor = [UIColor colorWithRed:1.0 green:155.0/255.0 blue:0.0 alpha:1.0];
self.playPauseButton.animationStyle = RSPlayPauseButtonAnimationStyleSplitAndRotate;
self.playPauseButton.paused = NO;
[self.playPauseButton addTarget:self action:#selector(playPauseButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:self.playPauseButton];
}
- (IBAction)rewind:(id)sender
{
//NSLog(#"currenttime: %f", CMTimeGetSeconds(self.player.currentTime));
//NSLog(#"timescale: %d", self.player.currentTime.timescale);
CMTime cmTime = CMTimeMake(CMTimeGetSeconds(self.player.currentTime) - 1.0, 1);
CMTime zero = CMTimeMake(1, 2);
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(zero)) {
[self.player.currentItem seekToTime:cmTime];
NSLog(#"!");
}
NSLog(#"RW");
}
- (IBAction)rewind2:(id)sender
{
/*
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) - 10.0, self.player.currentTime.timescale);
CMTime zero = CMTimeMake(0, 10000);
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(zero)) {
[self.player.currentItem seekToTime:cmTime];
self.rw2 += 1;
NSLog(#"RW2!");
}
*/
NSLog(#"RW2");
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) - 10.0, self.player.currentTime.timescale);
CMTime zero = CMTimeMake(1, 2);
NSLog(#"cmTime: %f", CMTimeGetSeconds(cmTime));
NSLog(#"zero: %f", CMTimeGetSeconds(zero));
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(zero)) {
[self.player.currentItem seekToTime:cmTime];
NSLog(#"!");
} else {
[self rewindAll];
}
}
- (IBAction)rewind3:(id)sender
{
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) - 60.0, self.player.currentTime.timescale);
CMTime zero = CMTimeMake(0, 10000);
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(zero)) {
[self.player.currentItem seekToTime:cmTime];
NSLog(#"RW3!");
}
NSLog(#"RW3");
}
- (void) rewindAll
{
CMTime one = CMTimeMake(1, 1);
[self.player.currentItem seekToTime:one];
}
- (IBAction)fastForward:(id)sender
{
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) + 5.0, self.player.currentTime.timescale);
NSDate *now = [NSDate date];
NSTimeInterval interval = [now timeIntervalSinceDate:self.now];
CMTime seekingCM = CMTimeMake(interval, 1);
if (CMTimeGetSeconds(cmTime) < CMTimeGetSeconds(seekingCM)) {
[self.player.currentItem seekToTime:cmTime];
}
}
- (IBAction)fastForward2:(id)sender
{
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) + 10.0, self.player.currentTime.timescale);
NSDate *now = [NSDate date];
NSTimeInterval interval = [now timeIntervalSinceDate:self.now];
CMTime seekingCM = CMTimeMake(interval, 1);
if (CMTimeGetSeconds(cmTime) < CMTimeGetSeconds(seekingCM)) {
[self.player.currentItem seekToTime:cmTime];
NSLog(#"FF2!");
} else {
[self fastForward3:nil];
}
}
- (IBAction)fastForward3:(id)sender
{
NSDate *now = [NSDate date];
NSLog(#"FIRSTDATE: %#", self.now);
NSTimeInterval interval = [now timeIntervalSinceDate:self.now];
interval -= 0.5f;
NSLog(#"INT: %f", interval);
CMTime seekingCM = CMTimeMake(interval, 1);
/*
NSTimeInterval interval = self.interval - 1.0;
CMTime seekingCM = CMTimeMake(interval, 1);
[self.player.currentItem seekToTime:seekingCM];
*/
//CMTime seekingCM = CMTimeMake(self.interval, 1);
[self.player.currentItem seekToTime:seekingCM];
NSLog(#"FF3!");
}
- (NSTimeInterval)currentPlaybackTime
{
return CMTimeGetSeconds(self.player.currentItem.currentTime);
}
- (void)setCurrentPlaybackTime:(NSTimeInterval)time
{
CMTime cmTime = CMTimeMakeWithSeconds(time, NSEC_PER_SEC);
[self.player.currentItem seekToTime:cmTime];
}
- (void)viewDidLayoutSubviews
{
[self.playPauseButton sizeToFit];
self.playPauseButton.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 1.2);
}
- (void)playPauseButtonDidPress:(RSPlayPauseButton *)playPauseButton
{
[playPauseButton setPaused:!playPauseButton.isPaused animated:YES];
if (playPauseButton.isPaused) {
[self.player pause];
}
else {
[self.player play];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
By the way, there is a UIlabel that gives the current time of the player, but if I leave the page and go back, the label automatically starts again at 00:00. This is because it's giving the current time of the new AVPlayer. Is there a way to make it give the current time of the original AVPlayer?
I would create a singleton object to control a AVPlayer, so no matter how many view controllers you have they all would communicate with this one player controller. So basically I'd advise you to move all the code you have in viewDidLoad to some MYPlayerController singleton class.
here is my view controller from swift, if it can help you, will be fine, I have implemented all radio stuff there:
swift view controller
I am making an application which will play audio.I have used a slider to show the time elapsed and time remaining graphically in the slider.But i am not getting how to get the value programitcically.I have used the following code but its throwing an exception.
-(void) updateMyProgress
{
float progress = [avPlayer currentTime]/[avPlayer duration];
self.myProgressView.progress = progress;
}
The code im my viewcontoller.m file is
#import "ViewController.h"
#interface ViewController ()
{
AVAudioPlayer *avPlayer;
AVPlayer *avp;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"naat" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:stringPath];
NSError *error;
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
[avPlayer setNumberOfLoops:2];
[avPlayer setVolume:self.sliderVolumeOutlet.value];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateMyProgress) userInfo:nil repeats:YES];
}
/*-(void) updateMyProgress
{
CGFloat progress = [avPlayer currentTime]/[avPlayer duration];
self.myProgressView.progress = progress;
}
*/
-(void) updateMyProgress
{
AVPlayerItem *currentItem = avp.currentItem ;
CMTime duration = currentItem.duration; //total time
CMTime currentTime = currentItem.currentTime; //playing time
CGFloat progress = currentTime.value/duration.value;
self.myProgressView.progress = progress;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sliderVolumeAction:(id)sender
{
UISlider *myslider= sender;
[avPlayer setVolume:myslider.value];
}
- (IBAction)stopButton:(id)sender
{
[avPlayer stop];
[avPlayer setCurrentTime:0];
}
- (IBAction)pauseButton:(id)sender
{
[avPlayer pause];
}
- (IBAction)playButton:(id)sender
{
[avPlayer play];
}
#end![enter image description here][1]
The attached image shows the screenshot of the exception
! [1]: http://i.stack.imgur.com/WsxtI.png
AVPlayer has no property called "duration".
"duration" is the property of AVPlayerItem.
And both duration and currentTime are of "CMTime" dataType
We have to get the value of CMTime then update the progress.
-(void) updateMyProgress
{
AVPlayerItem *currentItem = avPlayer.currentItem;
CMTime duration = currentItem.duration; //total time
CMTime currentTime = currentItem.currentTime; //playing time
CGFloat progress = currentTime.value/duration.value;
self.myProgressView.progress = progress;
}
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.
}
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).
-(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;
}
i have some issues with the AVFoundation music player.
1) It does not start instantly, i guess buffering is slow on the simulator?
2) It only starts with sound 50% of the time, the other 50% it does not start, very unreliable
Here is my class
Music_Player.h
#interface Music_Player : NSObject <AVAudioPlayerDelegate>
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
#property (nonatomic, retain) NSString *trackPlaying;
#property (nonatomic) BOOL isPlaying;
#property (nonatomic, retain) NSTimer *timer;
#property (nonatomic, retain) UISlider *slider;
-(void)initTrack: (NSString *) track;
-(void)startPlayer;
-(void)pausePlayer;
-(void)stopPlayer;
-(void)sliderUp;
-(void)sliderDown;
#end
Music_Player.m
#import "Music Player.h"
#implementation Music_Player
#synthesize audioPlayer;
#synthesize trackPlaying;
#synthesize timer;
#synthesize isPlaying;
#synthesize slider;
-(void)initTrack:(NSString *)track
{
/* Init slider */
self.isPlaying = FALSE;
self.trackPlaying = track;
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:self.trackPlaying ofType:#"mp3"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
[self.audioPlayer prepareToPlay];
/* Set slider max value */
self.slider.minimumValue = 0;
self.slider.maximumValue = self.audioPlayer.duration - 5;
}
-(void)startPlayer
{
if (self.isPlaying == TRUE)
{
NSLog(#"Pause clicked");
[self.audioPlayer pause];
self.isPlaying = FALSE;
} else {
NSLog(#"Play clicked");
[self.audioPlayer play];
self.isPlaying = TRUE;
if (![self.timer isValid]) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTime) userInfo:nil repeats:YES];
}
}
}
- (void)updateTime {
if (self.isPlaying == TRUE) {
NSTimeInterval currentTime = self.audioPlayer.currentTime;
NSLog(#"%f", currentTime);
// update UI with currentTime;
slider.value = round(currentTime);
}
}
-(void)pausePlayer
{
if (self.isPlaying == TRUE)
{
[self.audioPlayer pause];
self.isPlaying = FALSE;
}
}
-(void)stopPlayer
{
if (self.isPlaying == TRUE)
{
NSLog(#"Stop clicked");
[self.audioPlayer stop];
self.audioPlayer.currentTime = 0;
self.slider.value = round(self.audioPlayer.currentTime);
self.isPlaying = FALSE;
}
}
-(void)sliderUp
{
if (self.isPlaying == FALSE)
{
self.audioPlayer.currentTime = round(slider.value);
[self.audioPlayer play];
self.isPlaying = TRUE;
}
}
-(void)sliderDown
{
if (self.isPlaying == TRUE)
{
self.isPlaying = FALSE;
[self.audioPlayer stop];
}
}
/* AUDIO PLAYER DELEGATES */
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(#"Did finish with, %c", flag);
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(#"Error %#",error);
}
#end
I just set up the slider property and init it with a track from my viewController
/* Init Music */
self.music_player = [[Music_Player alloc] init];
self.music_player.slider = self.slider;
[self.music_player initTrack:#"track1"];
And then i just pass on the Btn clicks and slider value changes to the music_player class, what could be the issue? I will be testing it on a iPhone tomorow, so could it just be a simulator issue?
Two things:
In initWithData:error:, is there an error set after being called?
Why not use [AVAudioPlayer initWithContentsOfURL:error:]? Eg:
- (void)initTrack:(NSString *)track
{
self.isPlaying = NO;
self.trackPlaying = track;
NSBundle *mainBundle = ;
NSString *filePath = [[NSBundle mainBundle] pathForResource:self.trackPlaying ofType:#"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
}