I am trying to stream mp3 files and trying to buffer just like djromero says in this post it works just fine most of the times, but happens to get stuck afrer playerstatus changes to 1, randomly! Here’s the code:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (kRateDidChangeKVO == context) {
NSLog(@"Player playback rate changed: %.5f", self.player.rate);
if (self.player.rate == 0.0) {
if (self.player > 0) {
NSLog(@" . . . PAUSE");
}
}
} else if (kStatusDidChangeKVO == context) {
if(self.player.status == AVPlayerStatusFailed)
{
NSLog(@"failed");
NSLog(@"error:%@",self.player.error);
}
NSLog(@"Player status changed: %li", (long)self.player.status);
if (self.player.status == AVPlayerStatusReadyToPlay) {
NSLog(@" . . . ready to play");
}
} else if (kTimeRangesKVO == context) {
NSLog(@"Loaded time ranges changed");
NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey];
@try {
if(timeRanges != (id)[NSNull null])
if (timeRanges && [timeRanges count]) {
CMTimeRange timerange = [[timeRanges objectAtIndex:0] CMTimeRangeValue];
NSLog(@" . . . %.5f -> %.5f", CMTimeGetSeconds(timerange.start), CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration)));
//first time play
if (CMTIME_COMPARE_INLINE(timerange.duration, >, CMTimeMakeWithSeconds(10, timerange.duration.timescale))) {
if (!self.mediaModel.playingStatus) {
[self.player play];
self.mediaModel.isPlaying = YES;
self.mediaModel.playingStatus = 1;
}
}
//buffering paused
if (CMTIME_COMPARE_INLINE(timerange.duration, >, CMTimeMakeWithSeconds(15, timerange.duration.timescale))) {
if (self.mediaModel.playingStatus == 1) {
[self.player pause];
self.mediaModel.isPlaying = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:MYAPPS_NOTIFICATION_MEDIA_BUFFERING object:nil];
self.mediaModel.playingStatus = 2;
}
}
//play after buffer pause
if (CMTIME_COMPARE_INLINE(timerange.duration, >=, CMTimeMakeWithSeconds(20, timerange.duration.timescale))) {
if (self.mediaModel.playingStatus == 2) {
[self.player play];
[[NSNotificationCenter defaultCenter] postNotificationName:MYAPPS_NOTIFICATION_MEDIA_PLAYING object:nil];
self.mediaModel.playingStatus = 3;
self.mediaModel.isPlaying = YES;
}
}
//button press paused
if (CMTIME_COMPARE_INLINE(timerange.duration, >, CMTimeMakeWithSeconds(25, timerange.duration.timescale))) {
if (self.mediaModel.playingStatus == 4) {
[self.player pause];
self.mediaModel.isPlaying = NO;
self.mediaModel.playingStatus = 5;
}
}
}
}
@catch (NSException *exception) {
NSLog(@"player exception: %@", exception);
UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:nil message:NSLocalizedString(@"An error happened! please try again later", @"Error!TryAgainLater") delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertview show];
//[self killObservers];
}
@finally {
}
}
}
and here’s what I get from log:
Player status changed: 1
. . . ready to play
Player playback rate changed: 0.00000
. . . PAUSE
it can get stuck here forever! can you please help me with this?