I am making a little app that uses AVAudioPlayer to play an m4a or car audiobook. I am trying to save the current time the player is at when the viewer leaves the view so when they return they can pick up where they left off. I have created a slider that tracks the progress of the audio from beginning to end and can be moved back and forth by the viewer. I have made it so that when the viewer leaves the view the position of the slider is saved in the NSUserDefaults. I have checked and this is saved as a plist in the users preferences. I have been able to call up the plist file but I am not sure where to load it. I have updated my code since my first post. I someone could help me figure out where to load the save float value I would really appreciate it.
Here is my code.
I have tried loading the saved file in various places but with no luck.
Here is my code.
Code:
- (void)viewDidLoad
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
[super viewDidLoad];
self.title = myTitle;
NSString *pictureString = [[NSBundle mainBundle] bundlePath];
NSString *imageString =[pictureString stringByAppendingPathComponent:picture];
[self.button setBackgroundImage:[UIImage imageWithContentsOfFile:imageString] forState:UIControlStateNormal];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:self.myAudio ofType:nil]];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
theAudio.delegate =self;
theAudio.volume = 1.0;
theAudio.numberOfLoops = 0;
// Set the valueChanged target
[aSlider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)slide {
theAudio.currentTime = aSlider.value;
}
-(void)updateTime:(NSTimer *)timer {
aSlider.value = theAudio.currentTime;
}
-(IBAction)buttonPressed:(id)sender {
//This is where I get the value that was saved.
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
NSString *time = [NSString stringWithFormat:@"%f",[[ NSUserDefaults standardUserDefaults]floatForKey:@"playbackKey"]];
float place =[time floatValue];
NSLog(@"%f",place);
//I tried to load it into here but no go.
aSlider.maximumValue = [theAudio duration];
if ([theAudio isPlaying]){
[theAudio pause];
[button setTitle:@"Play" forState:UIControlStateNormal];}
else
{[theAudio play];
//I also tried here by putting [theAudio playAtTime:place] but didn't work
[button setTitle:@"Pause" forState:UIControlStateNormal];}
}
- (void)updateSlider {
aSlider.value = theAudio.currentTime;
}
- (IBAction)sliderChanged:(UISlider *)sender {
[theAudio stop];
[theAudio setCurrentTime:aSlider.value];
[theAudio prepareToPlay];
[theAudio play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{ if (flag) {
[sliderTimer invalidate];
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear: animated];
[theAudio stop];
NSString *time = [NSString stringWithFormat:@"%f",aSlider.value];
[[ NSUserDefaults standardUserDefaults]setObject:time forKey:@"playbackKey"];
}
I have tried loading the saved file in various places but with no luck.