So I have a video being accessed by filepicker.io that is attempting to be played with an MPMoviePlayerController. So far I've tried everything from trying to load the video directly from the FilePicker URL to grabbing the NSData and loading it into the local filesystem. This is my current code, but all that happens is I get a blank screen and the [movieplayer load state] returns 0 (error code). I've verified that the file is there and that it is an MOV filetype.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
movieString = [documentsDirectory stringByAppendingPathComponent:#"vid.mov"];
[responseData writeToFile:movieString atomically:YES];
NSURL *url = [NSURL fileURLWithPath:movieString];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];
[moviePlayer prepareToPlay];
[moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer play];
Declaring the movie player as a property fixed the problem. Apparently there's a bug associated with it.
Related
I would like to play a ts video file. The only simple way to do it is to save the ts url under an m3u file. That's what i did. The problem now is that AVPlayer don't play anything when i have this m3u file on my device. It plays it only when the file is saved on the web. I don't understand where is the problem..
Here is my code :
NSString* fileName = [NSString stringWithFormat:#"%ld.m3u",(long)indexPath.row];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *publicDocumentsDir = [paths objectAtIndex:0];
NSString *fullPath = [publicDocumentsDir stringByAppendingPathComponent:fileName];
NSURL *fileURL = [NSURL URLWithString:fullPath];
[AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer: layer];
[avPlayer play];
If i change
NSString* fileName = [NSString stringWithFormat:#"%ld.m3u",(long)indexPath.row];
with
NSString* fileName = #"http://www.media.com/url.m3u";
It will work fine...
Anyone to help me before i loose all my hairs?
I want to upload a video on cloudkit dashboard and display that video in an iOS app... searched about my query and came to know that using asset type we can do it.. but not getting exactly how to achieve it programmatically in Objective-c.
How can I do it programmatically in Objective-c?
Got solution for my problem.here is the code which I used to display video for those who may got such problem.
CKAsset * asset=[videoArray objectAtIndex:0];
NSData *urlData = [NSData dataWithContentsOfURL:asset.fileURL];
if ( urlData )
{
//save video
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename.mp4"];
[urlData writeToFile:filePath atomically:YES];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
//Play video
AVPlayer * avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, 300, 300);
[self.view.layer addSublayer: layer];
[avPlayer play];
}
I am working in an iOS project. I want may application to download a video from the internet programmatically. Then I want to play it. I know how can I play a local video from the Resources, but my question is how could I download it , and the find it to be played.
I am using MPMoviePlayerController to run the video.
Thanking in advance
I found the answer
here I saved the video
NSString *stringURL = #"http://videoURL";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *documentsDirectory ;
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"videoName.mp4"];
[urlData writeToFile:filePath atomically:YES];
}
and this code is for playing the video form its directory
NSString *filepath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"videoName.mp4"];
//video URL
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController play];
If you have a valid url of the video, Apple provides an API to directly buffer videos with NSURL.
You should hold a reference to the MPMoviePlayerController object from the controller so that ARC doesn't release the object.
#property (nonatomic,strong) MPMoviePlayerController* mc;
Make the URL
NSURL *url = [NSURL URLWithString:#"http://www.example.com/video.mp4"];
Init MPMoviePlayerController with that URL
MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] initWithContentURL:url];
Resize the controller, add it to your view, play it and enjoy the video.
self.mc = controller; // so that ARC doesn't release the controller
controller.view.frame = self.view.bounds;
[self.view addSubview:controller.view];
[controller play]; //Start playing
For more detail you can visit this playing video from a url in ios7
When I use the MPMoviePlayerController in my app, it does not play videos and just shows a black rectangle.
my code is as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = nil;
//filePath =[NSString stringWithFormat:#"%#/%#.mp4", documentsDirectory, fileName];
filePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp4",fileName]];
NSLog(#"%#",filePath);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists){
NSLog(#"exist!");
}else{
NSLog(#"no exist!");
}
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
[moviePlayer.view setFrame:self.view.frame];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.movieSourceType= MPMovieSourceTypeStreaming;
moviePlayer.fullscreen = YES;
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
-(void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
The output is:
iBeaconCollection[10320:60b] /var/mobile/Applications/107F6DE4-AE31-40E0-BAA0- 76A956B72116/Documents/101A ~ Hallway to 101-103.mp4
2014-06-11 14:13:27.302 iBeaconCollection[10320:60b] exist!
Hope someone could help me! Thanks.
Use MPMoviePlayerViewController, and then try and make sure that the file is in fact an MP4, as you are appending that extension.
So I'm using ASIHTTPRequest to download a video file from my FTP. It downloads and I get this as the directory path to the file:
/Users/Matt_Sich/Library/Application Support/iPhone Simulator/5.0/Applications/B7366FF2-7592-4ED0-ABC2-46BA111D0FF4/Documents/INWD.mp4
Ok now I want to play that video.... I would image that doing this would be easy but for some reason I can't manage to get it to work. All I get is a black view and that's it.
//NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Park_iPod" ofType:#"mp4"]];
NSURL *url = [NSURL URLWithString:self.PathString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
(NOTE: self.PathString is the path to the downloaded file that I mentioned above)
It plays the video from the main bundle just fine so I'm sure that there's something wrong with the file path.
Once you've downloaded your file you need to use fileURLWithPath to access the file, not URLWithString
NSURL *url = [NSURL fileURLWithPath.PathString];
This would get you the documents directory...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:([NSURL fileURLWithPath : self.PathString])];
[moviePlayer setShouldAutoplay:NO];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(movieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: moviePlayer];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object: moviePlayer];
[self.view addSubview: moviePlayer.view];
[moviePlayer.view setFrame: self.view.bounds];
[moviePlayer prepareToPlay];