开发者

AVPlayer not playing Video file

开发者 https://www.devze.com 2023-03-09 03:38 出处:网络
HI I\'m using AVPlayer to play the video slowly frame by frame. I used this coding for that. I could not able to play the video. Please notice my problem.

HI I'm using AVPlayer to play the video slowly frame by frame. I used this coding for that. I could not able to play the video. Please notice my problem.

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
newView.backgroundColor = [UIColor yellowColor];

NSString *videoName = [fileNameArray objectAtIndex:indexPath.section];
NSString *url = [Utilities documentsPath:[NSString stringWithFormat:@"OSC/%@/%@.mov",videoName,videoName]];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:url]];
//AVPlayer *avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:url]] retain];
AVPlayer *avPlayer = [[AVPlayer playerWithPlayerItem:playerItem] retain];
AVPlayerLayer *avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];

avPlayerLayer.frame = self.view.frame;
[newView.layer addSublayer:avPlayerLayer];
[self.view addSubview:newView];
[avPlayer play];

avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@select开发者_如何转开发or(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[avPlayer currentItem]];


Because you use local file, it's better to use [NSURL fileURLWithPath:url] for converting NSString to NSURL. So change the code to:

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:url]];


I don't know about frame by frame slowness, but you're doing a few things you don't need to do. In the iPhone documentation, the Apple guys completely messed with memory management, making it way more complex than it needs to be, so I'll tell you the key things that make it play:

First, retain the player, not the layer. If you don't know why, read a book or the Views Manual. (Which is filled with homonym-style typos, but it's easier than buying a thick book).

2nd, you did not cast the layer and use setPlayer:, the exact line is inside Apple's documentation. You have to convert self.layer using a typecast.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号