我正在尝试流式播放mp3文件并尝试缓冲,就像djromero中this post所说的那样 它在大多数时候都运行得很好,但是随后发生了因为球员的状态变化而被卡住了! 这是代码:
-(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 {
}
}
}
这是我从日志中得到的:
Player status changed: 1
. . . ready to play
Player playback rate changed: 0.00000
. . . PAUSE
它永远都会卡在这里!你可以帮我解决这个问题吗?