I am trying to have an audio only player in my App which can play in the background. Initially I used the MPMoviePlayerController, which worked fine, but the problem was that it doesn't support searching forward and backwards (or use a slider).
Some of the recordings are an hour long, and if somebody is interrupted during it and want to go back to the place they were they have to start from the beginning. Using AVAudioCoder seemed to me to be the solution so I changed my code to discover that it doesn't work with URL which are not on the device.
I do use Http Live Streaming, and have an .m3u8 file. Is there a easy to implement a solution to use .m3u8 and .mp3 files from an internet source with the ability to change the playback location using a slider. (and as it needs to play in the background, I cannot use the interface of the MPMoviePlayerController)
Update: Back using the MPMoviePlayerController, but some strange behavior. When playing the .m3u8 playlist, and setting the new position, the value given when querying the currentPlaybackTime is set to the right value, but the sound continues to play as if not interrupted.
When pausing the player, then setting the time it just ignores it and also keeps playing, but at least the 'currentPlaybackTime' gives the right current position.
id appDelegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate]; double length = [[appDelegate audioPlayer] duration]; if(length > 0){ float sliderValue = [_slider value]; double time = sliderValue / 100 * length;
NSTimeInterval playBackTime = time;
NSLog(@"Requested time: %f vs current time %f", playBackTime, [[appDelegate audioPlayer] currentPlaybackTime]);
//[[appDelegate audioPlayer] pause];
[[appDelegate audioPlayer] setCurrentPlaybackTime:playBackTime];
//[[appDelegate audioPlayer] setInitialPlaybackTime:playBackTime];
//[[appDelegate audioPlayer] play];
NSLog(@"Requested time: %f vs current time %f (2)", playBackTime, [[appDelegate audioPlayer] currentPlaybackTime]);
}
Every second I update the labels, and this gives me the behavior as described above:
id appDelegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
double time = [[appDelegate audioPlayer] currentPlaybackTime]; double length = [[appDelegate audioPlayer] duration]; NSLog(@"State: %i, Time: %f", [[appDelegate audioPlayer] playbackState], [[appDelegate audioPlayer] currentPlaybackTime]); if(length > 0){ double pos = time/length * 100; [_slider setValue:(float)pos]; [_slider setEnabled:true];
NSInteger seconds = (int) length % 60;
NSInteger minutes = ((int) length / 60) % 60;
double playableDuration = [[appDelegate audioPlayer] playableDuration];
if(playableDuration > 0 && playableDuration < length){
int downloaded = (int)((float)((float)playableDuration / (float)length) * 100);
[_unknownLength setText:[NSString stringWithFormat:@"Total duration: %02i:%02i (%02i%% downloaded)", minutes, seconds, downloaded]];
} else {
[_unknownLength setText:[NSString stringWithFormat:@"Total duration: %02i:%02i", minutes, seconds]];
}
} else {
[_slider setEnabled:false];
[_unknownLength setText:[NSString stringWithFormat:@"Total duration unknown"]];
}
NSInteger seconds = (int) time % 60; NSInteger minutes = ((int) time / 60) % 60; [_position setText: [NSString stringWithFormat:@"%02i:%02i", minutes, seconds]];