I’m trying to download a mp4 file from a server, then save it in a file. After it is saved in the file, I try to play it. When the program runs, it pauses when the file is being download, then the screen turns black when I create the MPMoviePlayerController object.
I did the following.
1. Traced through the code and view the NSDAta object to make sure the right anount of data was being load.
2. added a if ( [mFile fileExistsAtPath:filename]==true ) to make sure the file exist.
3. checked all the return values for a nil.
I’m left out of idears now!
// set up file name to save it under and play it
NSFileManager *mFile= [NSFileManager defaultManager];
NSString *filename=[ NSHomeDirectory() stringByAppendingPathComponent:@”ted10.dat”];
// load video
NSURL *movieUrl=[[NSURL alloc] initWithString:@"http://www.besttechsolutions.biz/projects/golfflix/ted.mp4"];
NSData *data = [NSData dataWithContentsOfURL:movieUrl];
[data writeToURL:movieUrl atomically:YES];
[mFile createFileAtPath:filename contents: data attributes:nil];
// makse sure file exist
if ( [mFile fileExistsAtPath:filename]==true )
{
// play it
NSURL *fileUrl=[ NSURL fileURLWithPath:filename];
mp=[[MPMoviePlayerController alloc] initWithContentURL: fileUrl];
[mp.view setFrame: self.view.bounds];
[self.view addSubview: mp.view];
if ([mp respondsToSelector:@selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
}//localhost/Dino/golflix2/Classes/videolist.h
else
{
}
[mp play];
}
Here’s your fixed code:
1)
Most importantly, you were originally trying to write your movie back to the remote URL. I think what you really wanted to do was write to a local file (or cache).
2)
The file extension (.mp4) gives a hint to the movie player what kind of file is playing. “.dat” is too generic.
3)
Speaking of writing to the local file, don’t put it in the home directory. Find the cache directory or somewhere else that’s more appropriate.