*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];
Related
I am using AvPlayer to play a video file.
NSString *strURL = #"Some URL";
NSURL *movieURL = [NSURL URLWithString:strURL];
self.p = [AVPlayer playerWithURL:movieURL];
self.controller = [[AVPlayerViewController alloc] init];
self.controller.player = self.p;
[self addChildViewController:self.controller];
[self.view addSubview:self.mp.view];
[self.p play];
This adds a default AVPlayerViewController to the AVPlayer.
I notice that if the user drags the seekbar within the bufferred zone.
My video play will instantly stuck. At this no user action can resume the video.
I am playing an HLS file. Do you guys know what might be causing the AVPlayer to stuck?
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.
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];
I am new at Swift Language. I want to develop simple movie player app for learning. I added some movies(".avi, .flv, .f4u.... which format is best) to my project. My view is just simple.There is a button on my view. ITs called "Play". When user tap on that button the movies will start.
Plus I need to play audio files too, like mp3 or .mid files. Can you give me how i can do that in Swift? IF you explain with example, It will be perfect.
Edit
I am not asking all code. I get confused when I try to convert my Objective C code to Swift. I want to know how I can develop below code in Swift?
NSString *path = [[NSBundle mainBundle] pathForResource:#"eminem1" ofType:#"mp4"];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path];
[self.view addSubview:myPlayer.view];
[myPlayer play];
Try this:
let path = NSBundle.mainBundle().pathForResource("eminem1", ofType: "mp4")
let pathURL = NSURL.fileURLWithPath(path)
let myPlayer = MPMoviePlayerViewController(contentURL: pathURL)
myPlayer.moviePlayer.repeatMode = .None
presentMoviePlayerViewControllerAnimated(myPlayer)
Don't forget import MediaPlayer.
Selected answer is deprecatedfor iOS 9+ versions.
Use this answer instead: How to play a local video with Swift?
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];