how to play mp4 file using html url IOS - iphone

I have been trying to play a mp4 file, I am receiving url from server, I have given that to MPMoviePlayerController, player is loading but file is not playing. Here is the code...
NSURL *url = [NSURL URLWithString:obj->msg_url]; // obj->url is a html php url not a direct file url.
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
So in this case how can I use MPMoviePlayerController to play from html url. Any idea?
thanks

Try
[moviePlayer play];
It seems shouldAutoplay does not always have the desired effect.

I have downloaded first and played its working now.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"sample.mp4"];
[urlData writeToFile:filePath atomically:YES];
NSURL *file_url = [NSURL fileURLWithPath:filePath];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:file_url];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];

Related

ios MPMoviePlayerController can't play local files on simulator

I'm new here. I'm already 2 days browsing for solutions, but unhappy.
Here is my code:
NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docaPathFull = [docPath stringByAppendingPathComponent:#"/IMG_0003.m4v"];
self.videoURL = [NSURL fileURLWithPath:docaPathFull];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnail = [player thumbnailImageAtTime:4.0 timeOption:MPMovieTimeOptionExact];
player = nil;
self.imageView.image = thumbnail;
Problem is, i can't make video file visible for MPMoviePlayerController. I can't get thumbnail and I can't play it, just black player and loading infinity.
I did try:
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"IMG_0003" ofType:#"m4v"];
self.videoURLtheurl=[NSURL fileURLWithPath:thePath];
and this
self.videoURL = [[NSBundle mainBundle] URLForResource:#"IMG_0005" withExtension:#"mov"];
and this
NSString *filePath = #"/Users/[user]/Documents/video/IMG_0004.mov";
self.videoURL = [NSURL fileURLWithPath:filePath];
If you have any ideas how to save my life, please do!!!
Many thanks for you time.
Actually this is my working example from my project. Make sure you added your m4v file to your project. The notification was a need for me, maybe it is not necessary for you.
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:
[NSURL fileURLWithPath: [[NSBundle mainBundle]
pathForResource:#"yourmovie" ofType:#"m4v"]]];
theMovie.view.frame = CGRectMake(0, 0, 320, 480);
[theMovie setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[self.view addSubview:theMovie.view];
[theMovie play];
You can use following method to get a thumbnail image for a given Video Url,
-(UIImage *) giveThumbofVideoUrl:(NSURL *) url{
NSString *urlString = [url absoluteString];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
//imageGenerator.maximumSize = CGSizeMake(320,480);
CMTime time = CMTimeMake(1, 1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return image;
}
How you can use it ?,
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"IMG_0003" ofType:#"m4v"];
NSURL *movieUrl=[NSURL fileURLWithPath:thePath];
UIImage *thumbnail = [self giveThumbofVideoUrl:movieUrl];
MPMoviePlayerController is Deprecated Now. So I have used AVPlayerViewController. and written the following code:
NSURL *videoURL = [NSURL fileURLWithPath:filePath];
//filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
Please do not forget to import following frameworks:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
Hope this Helps..

How to play video using MPMoviePlayerController?

I am using the following code to play the video using the MPMoviePlayerController , but the video is not played. Can anyone tell me why ?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"/one.mp4"];
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
[[moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [moviePlayer view]];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer play];
It's pretty weird, but it seems to work okay if you make your MPMoviePlayerController a property instead of a local variable. Seems something is going on behind the scenes. I'm thinking it's related to ARC. Are you using ARC?
It's also an issue that you've over-appended your path:
// You've already got the full path to the documents directory here.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"/one.mp4"];
// Now you're appending the full path to the documents directory to your bundle path
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
When I run your code in the simulator, the path looks like this:
/Users/mlong/Library/Application Support/iPhone
Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone
Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
It should just be this:
/Users/mlong/Library/Application Support/iPhone
Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
So just delete this line:
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
And then change your player instantiation to this:
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_moviePlayer play];
So you should add the MPMoviePlayerController as a property of your containing view controller.
All right, there is a big difference between the app bundle and the documents directory. I suggest you take a look at that.
First of all, Where is the video stored?
If your video is in the documents directory, don't append the documents directory path to the bundle path.
Just try with the filePath variable:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"/one.mp4"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]];
However, if the file is in the app bundle (you added it to your project in XCode), you should use what is in jinx response.
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(//set rect frame)];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
moviePlayer.repeatMode = NO;
[moviePlayer setFullscreen:YES animated:NO];
[moviePlayer prepareToPlay];
[self.view addsubview:movieplayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {
if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
//add your code
}
}
Try asking your bundle directly and not setting up the file path manually
NSString *path = [[NSBundle mainBundle] pathForResource:#"name" ofType:#"mov"];
Player is initialized with the URL of the video which we want to be played (It can be either a path of local file of a device or a live URL.) after that player is added as a sub view of current view.
Supported video formats by MPMoviePlayerController class are following
.mov .mpv .3gp .mp4
I am not sure how much you this article would help you out. I am new to this. I worked on the step by step instructions provided in this article
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
hope Help it
It took me few mins to debug the problem but the answer is quite simple. Here is the deal:
If you want to MPMoviePlayerViewController to play from a web URL use this:
NSURL *url = [NSURL URLWithString:#"https://www.test.com/anyMovie.mp4"];
And if you want MPMoviePlayerViewController to play it from App Bundle use this:
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"anyMovie" ofType:#"m4v"];
NSURL *url = [NSURL fileURLWithPath:moviePath];
The Rest of it goes the same, except you have to set this property "movieSourceType" as follows:
MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentViewController:moviePlayerView animated:YES completion:^{}];

Speed of Videos in iPhone App

For some reason the setcurrentPlayback Method does not work. Thanks for any thoughts...
-(IBAction)abspielen:(id)sender {
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:titleOfButton ofType:#"mov"];
NSURL *movieURL = [ NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *themovie =
[[MPMoviePlayerController alloc]initWithContentURL: movieURL];
[themovie play];
[themovie setCurrentPlaybackRate:2.f];
[themovie release];
MPMoviePlayerViewController *moviePlayer =
[[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer release];
}
add the line
themovie.rate=2.f;
insted of [themovie setCurrentPlaybackRate:2.f]; line
may be its work....
sometime in use of HTML blog,this method not work so just try given 1 line
May be it is useful to you....
:-)

Play video stored in NSData

I am trying to play a video that is stored in an NSData object. I save the file to my applications temp folder and then try to play it, but all I get is a black screen. I can later browse to that folder and play the file, so I know that the file gets written and is supported.
This is my code:
NSString *urlString = [NSTemporaryDirectory() stringByAppendingPathComponent:#"test.m4v"];
NSURL *url = [[NSURL alloc] initWithString:urlString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];
[moviePlayer prepareToPlay];
moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer play];
The only thing I can think of is that it takes some time for the file to get written, and the player tries to access it before it is done. Is that possible, and if so, how do I fix it?
When working with files you should tell the NSURL that the URL should point to a
file:
NSString *urlString = [NSTemporaryDirectory() stringByAppendingPathComponent:#"test.m4v"];
NSURL *url = [NSURL fileURLWithPath:urlString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];
[moviePlayer prepareToPlay];
moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer play];

iPhone: Trouble Playing Video in iOS 5

In my iPhone app, I am taking video using ImagePickerview and storing it to document directory
For iOS4 (device iPod) I can play video stored in document directory but for iOS 5 (device iPad) the same code is not working for playing video.
so for iOS 5 is there any different way for storing and playing video from document directory
(I also tried with library directory)
-(IBAction)saveVideo:(id)sender
{
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate=self;
if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagepicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagepicker setMediaTypes:[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]];
[imagepicker setCameraDevice:UIImagePickerControllerCameraCaptureModeVideo];
[self presentModalViewController:imagepicker animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSMutableString *tempPath = [[[info valueForKey:#"UIImagePickerControllerMediaURL"] absoluteString] mutableCopy];
NSString *newPath1 = [NSString stringWithFormat:#"myVideo.mov"];
[tempPath replaceOccurrencesOfString:#"file://localhost/private/" withString:#"/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempPath length])];
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], newPath1];
NSString *newPath = [NSString stringWithFormat:#"%#/%#", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], newPath1];
BOOL success = [manager copyItemAtPath:tempPath toPath:newPath error:&error];
if(error){
NSLog(#"New Path: %#", newPath);
NSLog(#"Error: %#", error);
}
if(success)
{
NSLog(#"Succeed");
}
[self dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
-(IBAction)playVideo:(id)sender
{
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:#"myVideo.mov"];
NSURL *url = [[NSURL alloc] initWithString:myDBnew];
NSLog(#"URL== %#",url);
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:#selector(moviePlayBackDidFinish:)
// name:MPMoviePlayerPlaybackDidFinishNotification
// object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
What could be wrong?
Here is the code.
If I add the video file into my project and use below code it is playing Video The only thing is it is not playing video from document directory
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"myVideo" ofType:#"mov"]];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
Even in iTunes via flittering I can see the file:"myVideo.mov", after running the app in device means This codes recording and storing video but not able to read or play.
Here in my code the problem was the way i am creating url
NSURL *url = [[NSURL alloc] initWithString:myDBnew];
which doesn't work for ios5 so i replaced in code by
NSURL *url = [[NSURL alloc] initFileURLWithPath:<#(NSString *)#> isDirectory:<#(BOOL)#>];