I get the above error with the following code:
NSURL *url = [NSURL fileURLWithPath:#"https://www.dropbox.com/s/crzu6yrwt35tgej/flexao.mp4?dl=1" isDirectory:NO];
self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:self.player];
When I copy the file to my bundle and load the url from there it works.
I guess it has something to do with my dropbox link, but I also don't know how to generate a different link or filename.
Modify your code as below
//You're using the URL https://www.dropbox.com/s/crzu6yrwt35tgej/flexao.mp4, but that's not a link to a video... it's a link to a page with a video on it.
To convert it to a direct link to the video, change www.dropbox.com to dl.dropboxuser.com, like this: https://dl.dropboxusercontent.com/s/crzu6yrwt35tgej/flexao.mp4
NSURL *url = [NSURL URLWithString:#"https://dl.dropboxusercontent.com/s/crzu6yrwt35tgej/flexao.mp4"];
self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:self.player];
Hope it fixes the issue..!
Related
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];
*Below two lines are for those who wants to use AVPlayer in their app.
This player works fine if i uncomment middle line in comments i.e NSURL *url and set the first line below comments i.e playerWithUrl:url , and comment the starting two lines.
*Below is My Real Problem, I need Help.
But i am not able to set URL the value to the player method-playerWithURL which is a first line below comments, and i am setting it through fileURL instance which i have created at starting in 2 line. IN the debugger it shows API path in *localfilepath. but when i am setting local file path to player through fileURL it shows nil.
But if i use middle line in comments i.e set *url for player everything works fine.
Help me to convert API into URL for that player property
NSString *localfilepath=[NSString stringWithFormat:#"%#",[managedObject valueForKey:#"clip_path"]];
NSURL *fileURL=[NSURL fileURLWithPath:localfilepath]; //because of this i am getting video URL but something #"file://" gets attached to it in the end thus not playing video URL because of that fileURLWithPath suggest something another way to fetch URL
// videoAVURLAsset = [AVURLAsset assetWithURL:fileURL];
// NSURL *url = [[NSURL alloc] initWithString:#"http://www.example.com/example_clips/SHAINA%20NC%20NEWS%20X%20180516%201657PM.mp4"];
// created player here //
player = [AVPlayer playerWithURL:fileURL]; //here i want to set the value which is in the *localfileurl
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];
[player play];
You should be using [NSURL fileURLWithPath:] to generate the URL object from a local file path string:
NSURL *fileURL = [NSURL fileURLWithPath:localfilepath];
Here is how I play a video in iOS:
First create a web view
Then link it to your view controller code
and then add this in the viewdidload()
//add the video to the player
let myYoutubeVideo = "https://www.youtube.com/embed/YNUEAxUGxyE"
let myHTMLString = "<iframe width=\"\(videoPlayer.frame.width)\" height=\"\(videoPlayer.frame.height)\" src=\"\(myYoutubeVideo)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>"
videoPlayer.allowsInlineMediaPlayback = true
videoPlayer.loadHTMLString(myHTMLString, baseURL: nil)
videoPlayer.frame = UIScreen.mainScreen().bounds
videoPlayer.center = self.view.center
videoPlayer.backgroundColor = UIColor.clearColor()
Try putting the file in this directory
#if TARGET_IPHONE_SIMULATOR
// where are you?
NSLog(#"Documents Directory: %#", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
#endif
This is the Documents directory for your app in the simulator. Then try to run it from there.
That way it should act like its pulling off the phone and not your local pc.
And give this a try too
file:///Users/codercat/Library/Developer/CoreSimulator/Devices/YourProjectDict/data/Containers/Data/Application/(sample digit)7F53CFB3-C534-443F-B595-62619BA808F2/Documents/your file located here
This one is for Xcode 7 and higher
i have made it guys :) i found the answer :) this is a complete solution if you have many video URL's and you want to play videos from server directly in your player so you need to fetch all your clip paths properly first and use the following code
// 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];
It is showing like this in browser,when I click on play it doesn't do anything.
But the same URL is working fine in Android native player.
Here is my code and URL of that video stored in server, format of video is MP4.
I tried with some other URLs like
NSURL *url=[[NSURL alloc] initWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
NSURL *urll = [[NSURL alloc]initWithString:#"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"];
These are working fine but below one is not working
NSURL *url = [[NSURL alloc]initWithString:#"http://****IP****.com:8888/alias_1440247177838"];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = CGRectMake(0, 20, self.videoPlayView.frame.size.width, self.videoPlayView.frame.size.height * (1.0f/3.0f)-20);
moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
[self.videoPlayView addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
Unfortunately MPMoviePlayer can only play video from a direct link. (similar to the ones that you mentioned in your question.
You can either:
use a direct link
Or switch to UIWebView instead of MPMoviePlayer
You can use a UIWebView in following way:
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:webView];
webView.allowsInlineMediaPlayback = YES;
NSURL* url = [NSURL URLWithString:#"http://****IP****.com:8888/alias_1440247177838"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Hope this helps!
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 am trying to play a video that is on my server. its short video. Nearly 2 minutes. i must download it and play whenever download finished. How can i do that. i havent opinion? do you know any article or code ? Thanks.
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 300, 400);
[moviePlayer play];`enter code here`