how to play the video in objective-c

0 votes
asked Sep 13, 2017 by jesto

I need to play the video in Xcode using objective-c. I have dragged an mp4 file in Xcode. And used the code:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"videoname" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[self.avPlayer play];

But showing the error as nil in filepath.

3 Answers

0 votes
answered Sep 13, 2017 by prashant-tukadiya

While adding item by drag and drop You should take Care following things

1) While drag and drop to XCode you must check check box copy item if needed

2) Check Target on right panel should be checked with your current target

0 votes
answered Sep 13, 2017 by vikas-rajput

try as

 NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"videoname" ofType:@"mp4"]; NSURL *streamURL = [NSURL fileURLWithPath:videoPath]; MPMoviePlayerController *moviplayer =[[MPMoviePlayerController alloc] initWithContentURL: streamURL]; [moviplayer prepareToPlay]; [moviplayer.view setFrame: self.view.bounds]; [self.view addSubview: moviplayer.view]; moviplayer.fullscreen = YES; moviplayer.shouldAutoplay = YES; moviplayer.repeatMode = MPMovieRepeatModeNone; moviplayer.movieSourceType = MPMovieSourceTypeFile; [moviplayer play]; 
0 votes
answered Mar 12, 2018 by user1374
  1. Drag and drop video file into your project and must check box copy item if needed
  2. Also add your video file in build phases copy bundle Resources

enter image description here

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...