Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer not playing Video file

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:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[avPlayer currentItem]];
like image 487
Ganesh Avatar asked May 31 '11 04:05

Ganesh


1 Answers

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]];
like image 162
Amir Kh. Avatar answered Sep 27 '22 21:09

Amir Kh.