我想在AvPlayer中播放远程.mp4视频。这是我的代码
NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *videoUrl = [[NSURL alloc]initWithString:encodedString];
self.asset = [AVAsset assetWithURL:videoUrl];
self.player = [AVPlayer playerWithURL:videoUrl];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.player currentItem]];
self.playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.myPlayerView.layer addSublayer:self.playerLayer];
self.videoPlaybackPosition = 0;
[self.player play];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.player && [keyPath isEqualToString:@"status"]) {
if (self.player.status == AVPlayerStatusFailed) {
NSLog(@"AVPlayer Failed --> %ld",(long)self.player.currentItem.status);
} else if (self.player.status == AVPlayerStatusReadyToPlay) {
NSLog(@"AVPlayerStatusReadyToPlay");
[self.player play];
} else if (self.player.status == AVPlayerItemStatusUnknown) {
NSLog(@"AVPlayer Unknown");
}
}
}
AVPlayerStatusReadyToPlay需要一些时间来缓冲视频,它只能工作一次。 但是我使用相同的代码录制视频并从临时文件播放。在那种情况下它工作正常。将视频上传到服务器后,当我尝试从服务器播放视频时,视频播放仅1-2秒。之后视频卡住,没有任何反应。我无法理解我的问题在哪里。
任何人都可以帮帮我吗?