MPMoviePlayerController does not work with movie in documents folder - ios

I have a ViewController which plays a movie. If the movie was downloaded (with ASI-HTTP-Request) it takes the local version. Otherwise it streams it from the web. It's the exact same movie.
Streaming does work very well, but playing the local file does not work. Just a black screen and no controls. The file has been downloaded correctly I checked the md5 checksum.
It's a Apple QuickTime Video (.mov) at a size of 1024x5xx pixel.
player = [[MPMoviePlayerController alloc] init];
[player.view setFrame: self.view.bounds];
if (local) {
// DOES not work
// SHOULD be [NSURL fileURLWithString:...]
[player setContentURL: [NSURL URLWithString: #"/path/to/the/movie.mov"]];
} else {
// does work
[player setContentURL: [NSURL URLWithString: #"http://mydomain.tld/path/to/the/movie.mov"]];
}
[self.view addSubview: player.view];
[player play]
I tried around with the players MediaSourceType. But it didn't help.
Why does it not work?
I don't have any error messages because I dont receive one..., is there a way to get error messages from it?
Solution
I found my error.
I used [NSURL URLWithString: #"/the/path/..."] for the local path.
It works with [NSURL fileURLWithPath: #"/the/path/..."]

Check URL to video file. This line return path to Documents directory.
NSString* docPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Your path is not related to Document directory
[player setContentURL: [NSURL URLWithString: #"/path/to/the/movie.mov"]];
Try this
[player setContentURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#%#", docPath, #"/path/to/the/movie.mov"]]];

Related

Could not load quick look data for "fileURL" unable to play AVPlayer video because of fileURLWithPath method

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];

could not load URL AVPlayer not playing video

*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];

Playing a sound in background

I have read some threads about this topic, and currently have this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soundFilePath = [NSString stringWithFormat:#"%#/testsoundsr.aiff",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath: soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
}
The sound is not played though, and I can only imagine that the problem is with the NSString definition which is used to reference the file
The file I'm trying to play is named 'testsoundsr.aiff' and I have dragged that into the project. Also I have added several frameworks to the project, including AudioToolBox.framework
---UPDATE---
I have now tried using an .mp3 file instead, but still getting no sound through.
Try player.delegate = self before [player play]

AVPlayer not playing local videos

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];

MPMoviePlayerController video not playing

I used MPMoviePlayerController in my application.
I got the right URL but MPMoviePlayer does not play video it shows only black screen.
My code is as below:
[video setImage:[UIImage imageNamed:#"video_active.png"] forState:UIControlStateNormal];
NSString *filename=[NSString stringWithFormat:#"%#.mp4",[appdel.TempVideoUrl valueForKey:tag]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *movieURL = [NSURL fileURLWithPath:filePath] ;
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(106, 18, 216, 235);
[uploadview addSubview:theMovie.view];
// Play the movie.
[theMovie pause];
I got the URL like this:
file://localhost/var/mobile/Applications/6E1D4CB7-A08D-438B-9FE7-E2FD9B7B5EEC/Documents/136_1355227629.mp4
Add [theMovie prepareToPlay]; before calling [theMovie play];.
prepareToPlay
Prepares a movie player for playback. (required)
- (void)prepareToPlay
Discussion
If a movie player is not already prepared to play when you call the
play method, that method automatically calls this method. However, to
minimize playback delay, call this method before you call play.
Calling this method may interrupt the movie player’s audio session.
For information on interruptions and how to resond to them, see Audio
Session Programming Guide. Availability
Available in iOS 3.2 and later.
Declared In MPMediaPlayback.h
Also you can use isPreparedToPlay for checking whether a movie player is ready to play.
Please refer this link for more.

Resources