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];
}
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 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
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'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.