我遇到了avplayer / avaudioplayer的问题。我使用这些类来传输aac或pls音频内容(无线电)。
我想预先加载指定时间内容并播放延迟播放,例如20秒。当我打开时,例如飞机模式和网络丢失,玩家应该在此时刻停止播放20秒。
我在网络上使用此代码用于mp4,它似乎有效,但是当我为aac / pls流式更改时...它正在加载并且一切似乎都有效但是当我打开飞行模式时,它会停止immidiately ...
是否可以保留缓冲区内容并通过流式播放延迟播放?
我的代码:
@interface AVFViewController ()
{
AVPlayer *player;
PlayerView *playerView;
id t
imeObserver;
NSInteger _playing;
}
@end
@implementation AVFViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self playURL:[NSURL URLWithString:@"http://nba.cdn.turner.com/nba/big/channels/top_plays/2012/02/03/20120203_top10.nba_nba_ipad.mp4"]];
//http://acdn.smcloud.net/t042-1_mp3.pls
//http://nba.cdn.turner.com/nba/big/channels/top_plays/2012/02/03/20120203_top10.nba_nba_ipad.mp4
}
- (void)playURL:(NSURL *)videoURL
{
if (!player) {
player = [[AVPlayer alloc] init];
playerView = [[PlayerView alloc] init];
[playerView setPlayer:player];
playerView.frame = CGRectInset(self.view.bounds, 20, 20);
playerView.backgroundColor = [UIColor greenColor];
playerView.alpha = 1;
[self.view addSubview:playerView];
[player addObserver:self forKeyPath:@"currentItem.loadedTimeRanges" options:NSKeyValueObservingOptionNew context:kTimeRangesKVO];
}
[player replaceCurrentItemWithPlayerItem:[[AVPlayerItem alloc] initWithURL:videoURL]];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (kTimeRangesKVO == context) {
NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey];
if (timeRanges && [timeRanges count]) {
NSArray *loadedTimeRanges = [[player currentItem] loadedTimeRanges];
CMTimeRange timeRange = [[loadedTimeRanges objectAtIndex:0] CMTimeRangeValue];
float startSeconds = CMTimeGetSeconds(timeRange.start);
float durationSeconds = CMTimeGetSeconds(timeRange.duration);
NSTimeInterval result = startSeconds + durationSeconds;
NSLog(@"duration is %f",result);
if (!_playing && result > 20) {
[player.currentItem seekToTime:CMTimeMakeWithSeconds(0, 1)];
[player play];
_playing = 1;
}
}
}
}
@end