I want to save the time to stop the audio and continue playing from the stop where the device is restarted. How can I do that?
I use this code, but I get an error.
CMTime targetTime = _audioPlayer.currentTime [self.audioPlayer seekToTime:targetTime]; Mistake:
Initializing 'CMTime' with an expression of incompatible type 'NSTimeInterval' (aka 'double')
How can I solve the issue?
UPD:
AudioPlayer.m
- (void) song{ if (_index == 0) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"1.mp3"]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO]; if (!fileExists) { NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39VHZ1cUZsM3BhQXM"; NSURL *url = [NSURL URLWithString:stringURL]; NSData *urlData = [NSData dataWithContentsOfURL:url]; [urlData writeToFile:filePath atomically:YES]; } self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; } else if (_index == 1) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"2.mp3"]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO]; if (!fileExists) { NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39R2Z5RlVWZkN3Vzg"; NSURL *url = [NSURL URLWithString:stringURL]; NSData *urlData = [NSData dataWithContentsOfURL:url]; [urlData writeToFile:filePath atomically:YES]; } self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; } else if (_index == 2) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"3.mp3"]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO]; if (!fileExists) { NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39a3JvWmZ6YTQ1NUk"; NSURL *url = [NSURL URLWithString:stringURL]; NSData *urlData = [NSData dataWithContentsOfURL:url]; [urlData writeToFile:filePath atomically:YES]; } self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; } } - (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]; if ([audioFile isEqualToString:@"2"]) { _index = 1; } else if ([audioFile isEqualToString:@"3"]) { _index = 2; } [self song]; } - (void)playAudio { [self.audioPlayer play]; } - (void)pauseAudio { [self.audioPlayer pause]; } - (BOOL)isPlaying { return [self.audioPlayer isPlaying]; } -(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; } - (void)setCurrentAudioTime:(float)value { [self.audioPlayer setCurrentTime:value]; } - (NSTimeInterval)getCurrentAudioTime { return [self.audioPlayer currentTime]; } - (float)getAudioDuration { return [self.audioPlayer duration]; }