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;
}
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'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];
}
I'm working on a voice memo app that records sound from user and plays them back.
Recording part works good. The recorded file was stored successfully & when a user taps on a saved file it just starts to play the file.
I'm using UISlider to indicate the play progress. Now, I disabled UISlider's userInteractionEnabled property to NO. So, I don't allow the user to interact with UISlider bar.
What do I need is, I want to enable this property to YES, so that one can choose the desired place to play the audio. For this, I added the following line in my slider code.
[mySlider addTarget:self action:#selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
By adding the above line, I can find the slider position within sliderChanged: method. But, I don't know how to initiate the player from the selected position. Each file has various sizes so their playing time also differs.
Is there any sample code available? Your help is needed.
Thanks
Sharing a solution in Swift.
I was able to get my project working with the slider based on the objective-c implementation zeeran posted, and it even updates the current time continuously now which mine wasnt doing before. I'm so pleased.
So I want to share the swift implementation in case it helps anyone out there.
import UIKit
import AVFoundation
import Foundation
class ActivityViewController: UIViewController, AVAudioPlayerDelegate {
/// The player & sound file object
var audioPlayer = AVAudioPlayer()
var activitySound = NSURL()
var updater : CADisplayLink! = nil // tracks the time into the track
var updater_running : Bool = false // did the updater start?
var playing : Bool = false //indicates if track was started playing
#IBOutlet var playButton: UIButton!
#IBOutlet var timeLabel: UILabel! // shows time and duration
#IBOutlet var audioSlider: UISlider! // slider object
#IBAction func pressedPlayButton(sender: AnyObject) {
if (playing == false) {
updater = CADisplayLink(target: self, selector: Selector("updateProgress"))
updater.frameInterval = 1
updater.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
updater_running = true
audioPlayer.play()
playButton.selected = true // pause image is assigned to "selected"
playing = true
updateProgress()
} else {
updateProgress() // update track time
audioPlayer.pause() // then pause
playButton.selected = false // show play image (unselected button)
playing = false // note track has stopped playing
}
}
// action for when the slider is moved: set as 'valuechanged' for UISlider object
#IBAction func sliderMoved(sender: UISlider) {
// if the track was playing store true, so we can restart playing after changing the track position
var wasPlaying : Bool = false
if playing == true {
audioPlayer.pause()
wasPlaying = true
}
audioPlayer.currentTime = NSTimeInterval(round(audioSlider.value))
updateProgress()
// starts playing track again it it had been playing
if (wasPlaying == true) {
audioPlayer.play()
wasPlaying == false
}
}
// Timer delegate method that updates current time display in minutes
func updateProgress() {
let total = Float(audioPlayer.duration/60)
let current_time = Float(audioPlayer.currentTime/60)
audioSlider.minimumValue = 0.0
audioSlider.maximumValue = Float(audioPlayer.duration)
audioSlider.setValue(Float(audioPlayer.currentTime), animated: true)
timeLabel.text = NSString(format: "%.2f/%.2f", current_time, total) as String
}
//- AVAudioPlayer delegate method - resets things when track finishe playing
func PlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
if flag {
playButton.selected = false
playing = false
audioPlayer.currentTime = 0.0
updateProgress()
updater.invalidate()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// configure AVAudioPlayer as audioPlayer object
let file = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("file1", ofType: "mp3")!)!
audioPlayer = audioPlayer(contentsOfURL: file, error: nil)
audioPlayer.delegate = self
audioSlider.continuous = false
}
// method makes sure updater gets stopped when leaving view controller, because otherwise it will run indefinitely in background
override func viewWillDisappear(animated: Bool) {
if playing == true {
audioPlayer.stop()
}
updater.invalidate()
updater_running = false
super.viewWillDisappear(animated)
}
}
before you load sound file refresh slider min and max values
yourSlider.minValue = 0.0;
yourSlider.maxValue = player.duration;
in method sliderChanged:
player.currentTime = yourSlider.value;
The following code sample should get you started. WIth this , the slider can function as a progress indicator and user can scrub it to start playback at specified position.
/* SliderMoved is invoked when slider value is changed- either when user scrubs it or when it is auto updated as audio playback progresses */
-(void)sliderMoved:(id)sender
{
[self.playbackTimer invalidate];
self.playbackTimer = nil;
NSInteger position = (NSInteger)self.slider.value * 60; // Assume the slider scale is in minutes- so convert to sec
if (self.isPlaying)
{
[self.audioPlayer pause];
}
[self.audioPlayer setCurrentTime:position];
if (self.isPlaying)
{
[self startOrResumePlaying];
}
[self updateSlider];
}
/* updateSlider is a helper function for updating slider value */
-(void)updateSlider
{
float total= self.audioPlayer.duration;
float f = (self.audioPlayer.currentTime) ;
self.slider.value = f/60.0;
self.progressLabel.text = [NSString stringWithFormat:#"%.2f",self.slider.value];
}
/* startOrResumePlaying starts audio playback and initializes a timer that auto increments the progress indicator every 3 sec */
-(void)startOrResumePlaying
{
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
self.isPlaying = YES;
self.playbackTimer=[NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:#selector(handleTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.playbackTimer forMode:NSDefaultRunLoopMode];
}
Mine is working by combining the above answers of rajagp and Igor Bidiniuc so I'm gonna vote them up both.
#import <AVFoundation/AVFoundation.h>
#property (nonatomic, strong) IBOutlet UIButton *playButton;
#property (nonatomic, strong) IBOutlet UISlider *slider;
#property (nonatomic, strong) IBOutlet UILabel *timeLabel;
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
#property (nonatomic, strong) NSTimer *timer;
- (IBAction)playPause:(id)sender;
- (IBAction)sliderValueChanged:(id)sender;
Here goes the implementation file.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.slider.continuous = YES;
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
if (error == nil) {
NSLog(#"audio session initialized successfully");
[self PlayAudio:#"fileName"];
} else {
NSLog(#"error initializing audio session: %#", [error description]);
}
}
- (void)PlayAudio:(NSString*)file {
NSError *error = nil;
NSString *soundPath =[[NSBundle mainBundle] pathForResource:file ofType:#"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundPath];
if ([self.audioPlayer isPlaying]) {
[self.audioPlayer pause];
[self.timer invalidate];
}
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
self.audioPlayer.delegate = self;
if (error == nil) {
NSLog(#"audio player initialized successfully");
[self playPause:nil];
} else {
NSLog(#"error initializing audio player: %#", [error description]);
}
}
- (void)viewWillDisappear:(BOOL)animated {
if ([self.audioPlayer isPlaying]) {
[self.audioPlayer pause];
[self.timer invalidate];
}
[super viewWillDisappear:animated];
}
-(IBAction)playPause:(id)sender
{
if ([self.audioPlayer isPlaying]) {
[self.audioPlayer pause];
self.playButton.imageView.image = [UIImage imageNamed:#"play"];
[self.timer invalidate];
} else {
[self.audioPlayer play];
self.playButton.imageView.image = [UIImage imageNamed:#"pause"];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateProgress) userInfo:nil repeats:YES];
}
}
- (IBAction)sliderValueChanged:(id)sender {
[self.timer invalidate];
self.timer = nil;
float position = self.slider.value;
if (self.audioPlayer.isPlaying)
{
[self playPause:nil];
}
[self.audioPlayer setCurrentTime:position];
if (self.audioPlayer.isPlaying)
{
[self playPause:nil];
}
[self updateProgress];
}
#pragma mark - Timer delegate
-(void)updateProgress
{
NSInteger durationMinutes = [self.audioPlayer duration] / 60;
NSInteger durationSeconds = [self.audioPlayer duration] - durationMinutes * 60;
NSInteger currentTimeMinutes = [self.audioPlayer currentTime] / 60;
NSInteger currentTimeSeconds = [self.audioPlayer currentTime] - currentTimeMinutes * 60;
NSString *progressString = [NSString stringWithFormat:#"%d:%02d / %d:%02d", currentTimeMinutes, currentTimeSeconds, durationMinutes, durationSeconds];
self.timeLabel.text = progressString;
self.slider.minimumValue = 0.0;
self.slider.maximumValue = [self.audioPlayer duration];
[self.slider setValue:[self.audioPlayer currentTime] animated:YES];
}
#pragma mark - AVAudioPlayer delegate methods
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag) {
self.playButton.imageView.image = [UIImage imageNamed:#"play"];
[self.timer invalidate];
}
}
AVAudioPlayer has a currentTime property you can set. Please refer to AVAudioPlayer documentation.