for my app I want the user to record a video which will be stored in the caches directory for repeated use throughout the application.
NSString *DestFilename = # "output.mov";
//Set the file save to URL
NSLog(#"Starting recording to file: %#", DestFilename);
NSString *DestPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
DestPath = [paths objectAtIndex:0];
DestPath = [DestPath stringByAppendingPathComponent:DestFilename];
NSURL* saveLocationURL = [[NSURL alloc] initFileURLWithPath:DestPath];
[MovieFileOutput startRecordingToOutputFileURL:saveLocationURL recordingDelegate:self];
The NSLog says:
url = file:///var/mobile/Containers/Data/Application/BD41ABB0-77BA-4872-B447-07906A3C6FA7/Library/Caches/output.mov
How can I load or possibly embed this video into one of the ViewControllers in my storyboard?
Basically, your video URL is there, so you can load the video into your view controller and play it in 2 possible approaches: using MPMoviePlayerController or AVPlayer
For example from your view controller
//The first approach
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:saveLocationURL];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, 1024, 768)];
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController play];
//The second approach
self.playerItem = [AVPlayerItem playerItemWithURL:saveLocationURL];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer:layer];
[self.player play];
Hope this will help you.
Related
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.
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
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];
Drag and drop video file into your project and must check box
copy item if needed
Also add your video file in build phases copy bundle Resources
I am getting video data from server. i am using signalR Framework. first of all i am capturing video from iPhone photo library. Then next i am converting encoding format (base64EncodedStringwithOptions) method. next i am sending video encrypted data to signalR server. and next i am receiving video data and decrypted and play the video data using AVPlayer. but my problem video does not played. but i received video data from signalR. For decrypting i am using this method. (NSDataBase64DecodingIgnoreUnknownCharacters)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *videoFile=[documentDirectory stringByAppendingPathComponent:post.mediaName];
NSURL *url;
if ([[NSFileManager defaultManager] fileExistsAtPath:videoFile]) {
url=[NSURL URLWithString:videoFile];
}
NSLog(#"Video URL is:%#",url);
AVPlayer *player = [AVPlayer playerWithURL:url];
cell.videoPlayerController.player = player;
[player play];
i got Video URL like this:
Video URL is:/Users/Library/Developer/CoreSimulator/Devices/EEC96DE7-6D0F-4D80-82B8-3C24E4F6B3EF/data/Containers/Data/Applicatication video0006.mp4
You can use MPMoviePlayerController to play video from url' but make sure that you have to declare property for that like,
#property MPMoviePlayerController *videoController;
don't create local object of MPMoviePlayerController.
you can use it something like,
self.videoController = [[MPMoviePlayerController alloc] init];
[self.videoController setContentURL:tempView.videoURL]; //here your url
[self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.videoController.view];
// close or cancel button if you want to put
UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60, 20, 60, 30)];
[closeButton addTarget:self action:#selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
[closeButton setTitle:#"Cancel" forState:UIControlStateNormal];
[self.videoController.view addSubview:closeButton];
// add observer to notify when playing will be completed if you want to put
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoController];
[self.videoController play];
Update :
You must use file url like,
NSURL *url = [NSURL fileURLWithPath:videoFile];
instead of
[NSURL URLWithString:videoFile];
Looking to add a video to my apps intro screen like the Uber app. I am sure there are others.
I found this which uses a gif and UIWebView, but not sure if this is the best solution. I definitely don't want to use images and stitch them together (would rather not have video if that is the preferred method).
https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2
This is what I use in my app Letsplay, The answer from Aslam will work but uses MPmoviePlayerController which is depreciated as of IOS 9.0.
Also I set the video gravity so that your video will fill the entire frame which means no black borders.
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"icebergs" ofType:#"mp4"];
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath: videoPath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:[AVURLAsset URLAssetWithURL:videoURL options:nil]];
AVPlayer* videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer* videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:videoPlayer];
[videoPlayerLayer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view.layer addSublayer:videoPlayerLayer];
[videoPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[videoPlayer play];
If you don't want sound then remember to mute the player
[videoPlayer setMuted:YES];
I haven't tried the code, but I it should work. It generally takes the path of the video file and adds it to the view controller. You may have to set the coordinates and size. Though I have used here video, GIF is more preferred.
- (void)viewDidAppear:(BOOL)animated
{
[self loadVideoInBackgroundOfView];
}
-(void)loadVideoInBackgroundOfView{
MPMoviePlayerController* videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getTheVideoPath]];
[videoPlayer.view setFrame:self.view.bounds];
[self.view insertSubview:moviePlayer.view atIndex:0];
[videoPlayer prepareToPlay];
[videoPlayer play];
}
-(NSURL*)getTheVideoPath{
NSString *path = [[NSBundle mainBundle] pathForResource:#"VideoFileName" ofType:#"mp4"];
NSURL *URL = [NSURL fileURLWithPath:path];
return URL;
}
I am trying to play a video which is stored on my device using the following code. The problem is that I am seeing nothing on the screen, just a white screen.
The value of filePath in this case is file:///var/mobile/Containers/Data/Application/0E87EF09-B87C-443F-9BFB-E9A68AC4A162/Documents/TMRecordedVideos/2015-07-25T18-57-19Z.mov
NSString *filePath = [self.video fileURL];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath isDirectory:NO];
AVAsset *videoAsset = [AVAsset assetWithURL:fileUrl];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:videoAsset];
self.videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
[self.videoPlayerLayer setFrame:self.view.frame];
[self.view.layer addSublayer:self.videoPlayerLayer];
[self.videoPlayer play];
I am using AVPlayerLayer for playing video. I used the below code and it worked fine. The URL(in my case outputFileURL) is similar to your filePath.
AVPlayer *avPlayerq = [AVPlayer playerWithURL:outputFileURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];
Hope this may help you to sort out your issue.
I've been struggling with this for some hours...
I'm trying to play a video I download and save using AVPlayer but nothing shows up.
What surprised me the most is that if I use Apple's sample url for testing http live streaming
that video does play. (http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8)
However, when I change the url to the local file path the video doesn't play. I recorded the video with the iPhone and verified that the format (.mov) is playable. Here is the code block:
AVPlayer *mPlayer = [[AVPlayer alloc] init];
//mPlayer = [mPlayer initWithURL:[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
mPlayer = [mPlayer initWithURL:filePath];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:mPlayer];
[[[self view] layer] insertSublayer:playerLayer atIndex:3];
[playerLayer setFrame:self.view.bounds];
[mPlayer play];
NSLog(#"Playing video at: %#", filePath);
Sample output:
Playing video at: /var/mobile/Applications/B298EE7D-D95D-4CCC-8466-2C0270E2071E/Documents/394665262.174180.mov
EDIT:
Solved by using [NSURL fileURLWithPath:local_url_string]
Just small piece of code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"splash" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
AVPlayer* player = [AVPlayer playerWithURL:videoURL];