How to play Audio from the custom position of UISlider? - ios

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.

Related

Update Song time label

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 :)

Playing audio while applying video filter in GPUImage

I've googled this question but did not find any solution.
So, my problem is,
I'm applying video filter using GPUImage to a video file. At same time, sound of that video is not playing. I know that sound playing is not supported in GPUImage while applying filters.
So, how can I achieve that?
You can achieve sound playing by adding support for AVPlayer like this.
In GPUImageMovie.h,
#property(readwrite, nonatomic) BOOL playSound;
In GPUImageMovie.m, update #interface GPUImageMovie () like this.
#interface GPUImageMovie() <AVPlayerItemOutputPullDelegate>
{
// Add this
BOOL hasAudioTrack;
.........
........
// Add all below three lines
AVPlayer *theAudioPlayer;
CFAbsoluteTime startActualFrameTime;
CGFloat currentVideoTime;
}
After that, in -(void)startProcessing method, add following lines.
- (void)startProcessing
{
// Add this
currentVideoTime = 0.0f;
.....
.....
// Add this
if (self.playSound)
{
[self setupSound];
}
GPUImageMovie __block *blockSelf = self;
[inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:#"tracks"] completionHandler: ^{
runSynchronouslyOnVideoProcessingQueue(^{
.........
.........
// Add this
startActualFrameTime = CFAbsoluteTimeGetCurrent() - currentVideoTime;
.......
.......
});
}];
}
Again, In -(AVAssetReader*)createAssetReader update it,
- (AVAssetReader*)createAssetReader
{
.......
.......
BOOL shouldRecordAudioTrack = (([audioTracks count] > 0) && (self.audioEncodingTarget != nil));
// Add this
hasAudioTrack = ([audioTracks count] > 0);
.........
}
In - (void)processAsset,
- (void)processAsset
{
.......
.......
if ([reader startReading] == NO)
{
NSLog(#"Error reading from file at URL: %#", self.url);
return;
}
// Add this
if (self.playSound && hasAudioTrack)
{
[theAudioPlayer seekToTime:kCMTimeZero];
[theAudioPlayer play];
}
.........
.........
}
In -(void)endProcessing,
- (void)endProcessing
{
.........
.........
if (self.playerItem && (displayLink != nil))
{
[displayLink invalidate]; // remove from all run loops
displayLink = nil;
}
// Add this
if (theAudioPlayer != nil)
{
[theAudioPlayer pause];
[theAudioPlayer seekToTime:kCMTimeZero];
}
.........
.........
}
Add new method -(void)setupSound at last,
// Add this
- (void)setupSound
{
if (theAudioPlayer != nil)
{
[theAudioPlayer pause];
[theAudioPlayer seekToTime:kCMTimeZero];
theAudioPlayer = nil;
}
theAudioPlayer = [[AVPlayer alloc] initWithURL:self.url];
}
At last, when you create your GPUImageMovie object, don't forget to set playSound flag to YES.
Like this : movieFile.playSound = YES;
Hope this helps.
how to pause the audio during filter applied by GPUImage?
Like we can pause the video writing by movieWriter.paused = YES; when I pause the movie writing, I need to Pause audio also.

why am i getting progress slider exception error

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;
}

Update UIProgressView based on how much of an mp3 has played

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;
}

IOS issues with AVFoundation music player, working 50% of the time

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

Resources