Sti*_*Sti 12 cocoa-touch objective-c avfoundation ios avplayer
当玩家开始播放外部视频(通过互联网)使用时,我在注册时遇到了一些麻烦AVPlayer.在提出解决方案之前,请先阅读问题.我像这样初始化播放器:
player = [[AVPlayer alloc] initWithURL:[[NSURL alloc] initWithString:@"http://example.com/video.mp4"]];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[playerLayer setFrame:[videoView bounds]];
[videoView.layer addSublayer:playerLayer];
Run Code Online (Sandbox Code Playgroud)
这会将播放器正确添加到视图中.我添加了以下两行代码来跟踪播放器何时准备好以及状态/速率是多少;
[player addObserver:self forKeyPath:@"rate" options:0 context:nil];
[player addObserver:self forKeyPath:@"status" options:0 context:nil];
Run Code Online (Sandbox Code Playgroud)
- (void)observeValueForKeyPath:....当某些内容随着状态或速率发生变化时,这两行将调用该方法AVPlayer.
到目前为止它看起来像这样:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//To print out if it is 'rate' or 'status' that has changed:
NSLog(@"Changed: %@", keyPath);
if ([keyPath isEqualToString:@"rate"]) //If rate has changed:
{
if ([player rate] != 0) //If it started playing
{
NSLog(@"Total time: %f", CMTimeGetSeconds([[player currentItem] duration]));
// This NSLog is supposed to print out the duration of the video.
[self setControls];
// This method (setControls) is supposed to set play/pause-buttons
// as well as labels for the current and total time of the current video.
}
}
else if ([keyPath isEqualToString:@"status"]) // If the status changed
{
if(player.status == AVPlayerStatusReadyToPlay) //If "ReadyToPlay"
{
NSLog(@"ReadyToPlay");
[player play]; //Start the video
}
}
}
Run Code Online (Sandbox Code Playgroud)
该state的AVPlayer变化readyToPlay几乎立即初始化之后,我再调用[player play].当发生这种情况时,rate改变1.00000,意味着它实际上以该速率播放,但视频现在才开始缓冲,而不是播放.屏幕是黑色的,需要几秒钟,然后开始播放.然而,该速率表明它开始播放之前.速率保持在1.00000,而不是0在开始缓冲时,这使得我很难知道播放器何时有足够的信息来开始设置控件(IE时间戳等).
在NSLog()打印出的视频上面打印出的持续时间nan(非数字),这使我想到,该项目还没有准备好进行播放,但是,速度停留在1.0000直到缓冲了一段时间,然后它实际上将玩,仍然与率1.0000.
然而,它会被调用两次.在rate"转变",1.0000 两次没有被别的之间.在两个呼叫中,视频的持续时间都是可用变量.
我的目标是尽可能快地获取视频的当前和总时间戳(IE 0:00/3:52).这也将用于注册滑块的擦洗(用于快进等).
当玩家通知我它以1.0000两倍的速度播放时,这些值还没有准备好.如果我在一秒左右(和通话[player play])后手动点击"播放" ,那么它正在工作.如何注册以了解视频何时准备就绪,而不仅仅是"准备好准备"?
请参阅AVPlayer上的addBoundaryTimeObserverForTimes:queue:usingBlock:以及Apple的此示例.
AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
[player play];
// Assumes a property: @property (strong) id playerObserver;
// Cannot use kCMTimeZero so instead use a very small period of time
self.playerObserver = [player addBoundaryTimeObserverForTimes:@[[NSValue valueWithCMTime:CMTimeMake(1, 1000)]] queue:NULL usingBlock:^{
//Playback started
[player removeTimeObserver:self.playerObserver];
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3909 次 |
| 最近记录: |