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 :)
Related
I am creating a game application in which I need to set timer of 2 minutes throughout the app screens in objective c.
I am creating it in viewDidLoad but it is creating a new instance every-time the view loads.
Here is the code which I am using :
#interface SomeViewController ()
{
int timerCounter;
NSTimer *timer;
}
#property (strong, nonatomic) IBOutlet UILabel *timerLbl;
#end
#implementation SomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startCountdown];
}
- (void)startCountdown
{
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(countdownTimer:)
userInfo:nil
repeats:YES];
}
- (void)countdownTimer:(NSTimer *)timer
{
timerCounter--;
int minutes = timerCounter / 60;
int seconds = timerCounter % 60;
NSString *string = [NSString stringWithFormat:#"%02d", minutes];
NSString *string2 = [NSString stringWithFormat:#"%02d", seconds];
NSString *timeTotal = [string stringByAppendingString:#":"];
NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];
_timerLbl.text = timeTotal2;
if (timerCounter <= 0) {
[timer invalidate];
}
}
You need to invalidate it whenever the VC deallocates
- (void)dealloc {
[timer invalidate];
}
//
The second VC may look like this
#import "ggViewController.h"
NSInteger timerCounter = 120; // declared global to hold the value
#interface ggViewController ()
{
NSTimer*timer;
}
#end
#implementation ggViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// instead of using selector use this inline callback
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:true block:^(NSTimer * timer) {
timerCounter--;
int minutes = timerCounter / 60;
int seconds = timerCounter % 60;
NSString *string = [NSString stringWithFormat:#"%02d", minutes];
NSString *string2 = [NSString stringWithFormat:#"%02d", seconds];
NSString *timeTotal = [string stringByAppendingString:#":"];
NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];
_timerLbl.text = timeTotal2;
if (timerCounter <= 0) {
[timer invalidate];
}
}];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[timer invalidate];
}
- (IBAction)gg:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
}
#end
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 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];
}