I am currently trying to retrieve an .mp4 from a URL.
The video is accessible from a web-browser but the play button is crossed out. Any ideas on what could be going wrong?
Code in question
NSURL *movieURL = [NSURL URLWithString:self.videoURL];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:movieURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;
Screenshot of device
I think there is no way apart from UIWebView to play youtube video inside iOS application. Also you can redirect to youtube application by passing your video url.So youtube application will handle rest of the things
You can read the guidelines of YouTube section 2 point 10 here
You can use youtube player here
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"IMG_0597" ofType:#"mov"]];
AVURLAsset *asset = [AVURLAsset assetWithURL:movieURL];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
[playerViewController.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)];
playerViewController.delegate=self;
playerViewController.showsPlaybackControls = YES;
[self.view addSubview:playerViewController.view];
[player play];
The reason why my videos did not play was because in the xml, where I was retrieving the url for the videos, I did not start the url with http://.
The code for this player works as it should.
Related
I have made one sample demo
Play video on my simulator using AVPlayer.
But my problem is video is not showing where audio of that video is working.
Code is:
#property (nonatomic,strong)AVPlayerViewController *videoPlayer;
-(IBAction)playVideo:(id)sender {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"video" ofType:#"mp4"]];
AVPlayer *player = [AVPlayer playerWithURL:url];
self.videoPlayer = [[AVPlayerViewController alloc]init];
[self presentViewController:self.videoPlayer animated:YES completion:nil];
self.videoPlayer.player = player;
self.videoPlayer.showsPlaybackControls=YES;
self.videoPlayer.view.frame = self.view.frame;
[self.view addSubview:self.videoPlayer.view];
[player play];
}
Image
I guess that the problem is in your video file. Try to download another video mp4 video sample. Add it to your project and try.
You don't need this code:
self.videoPlayer.view.frame = self.view.frame;
[self.view addSubview:self.videoPlayer.view];
I am using Xcode 7 and with quick look feature i encountered this error. It says could not load quick look data for "fileURL" here is my code
// here because of below line #file gets attached to the end of my fileURL like this --
#"http:/example.com/some_clips/SHAINA%20NC%20NEWS%20X%20180516%201657PM.mp4 -- file:///" see here something file:/// in the end so because of this i am unable to play video.
IF i use initWithString and give URL directly then it gives no error please suggest me something to set URL to my player below i have created please read comment in the code also. Do anyone have idea about this.
NSString *localfilepath=[NSString stringWithFormat:#"%#",[managedObject valueForKey:#"clip_path"]]; //here i am getting my URL path upto .mp4 but after below line word -- file gets attached to it because of fileURLWithPath method don't know how to convert that
NSURL *fileURL = [NSURL fileURLWithPath:localfilepath]; //suggest me this line of code so that i can pass URL to the player here this method is not working
// create a player
player = [AVPlayer playerWithURL:fileURL]; //here this fileURL which is going to player is going with that file:/// so giving error and no video runs
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(10,80,300,300);
controller.player = player;
controller.showsPlaybackControls = YES;
[player pause];
You should use:
NSURL *fileURL = [NSURL URLWithString:localfilepath];
Instead of:
NSURL *fileURL = [NSURL fileURLWithPath:localfilepath];
The fileURLWithPath: method is used for creating local file (Files that are situated in the device itself) urls.
i have made it guys :) i found the answer :) This is complete code for playing videos into AVPlayer which are on server
// here i am fetching all the url downloaded from server using core data managed objects
NSString *localfilepath=[NSString stringWithFormat:#"%#",[managedObject valueForKey:#"clip_path"]];
// then on below line i am encoding it into NSUTF8StringEncoding format
don't forget to do this otherwise video will not play as it wasn't in my case
NSURL *url = [NSURL URLWithString:[localfilepath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// and below i have assigned url to the player and created the player
player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(10,80,300,300);
controller.player = player;
controller.showsPlaybackControls = YES;
//player.closedCaptionDisplayEnabled = NO;
[player pause];
[player play];
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;
}
A video from the internet like so:
NSURL *url = [NSURL URLWithString:#"http://www.example.com/myvideo.m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];
Unable to play movie on Simulator Black Screen displays
You should try some frameworks (for example HCYoutubeParser) that can fetch a direct link to video from Youtube for you.
// Gets an dictionary with each available youtube url
NSDictionary *videos = [HCYoutubeParser h264videosWithYoutubeURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=8To-6VIJZRE"]];
// Presents a MoviePlayerController with the youtube quality medium
MPMoviePlayerViewController *mp = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[videos objectForKey:#"medium"]]] autorelease];
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];