Following code used to save videos to photo Album.
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
NSString *sourcePath = [[info objectForKey:#"UIImagePickerControllerMediaURL"]relativePath];
UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
}
If it is video i want to play it if it is image i want to display it.
Help Me.
with the code check if is image or video :
NSString *mediaType1 = [info objectForKey:UIImagePickerControllerMediaType];
NSLog(#"mediaType : %#",mediaType1);
if ([mediaType1 isEqualToString:#"public.image"])
{
//Show Image
}
else
{
//show Video
}
To Play the video check the URL.
EDIT:
NSString *moviePath = [bundle pathForResource:#"IMG_0017" ofType:#"MOV"];
NSLog(#"moviePath : %#",moviePath);
// NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
NSURL *movieURL = [NSURL URLWithString:strValURL];
NSLog(#"movieURL : %#",movieURL);
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2)
{
NSLog(#"> 3.2");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
if (mp)
{
[self presentMoviePlayerViewControllerAnimated:mp];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[mp.moviePlayer play];
[mp release];
}
}
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2)
{
NSLog(#"< 3.2");
theMovie = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
[theMovie play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerViewController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer release];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player autorelease];
}
In your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method, you can get the url of the video and play it:
#property (nonatomic,retain) MPMoviePlayerController *moviePlayerController;
if ([mediaType isEqualToString:#"public.movie"])
{
NSURL *aURL = [info objectForKey:UIImagePickerControllerMediaURL];//get the url
// and init the video player using this url
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:aURL];
[self.view _moviePlayerController.view];
_moviePlayerController.useApplicationAudioSession = NO;
_moviePlayerController.fullscreen = YES;
[_moviePlayerController play];
}
Of course, you'll have to import the MediaPlayer.framework
EDIT: A majority of Cocoa projects now use arc, so the above code would require you retain the MPMoviePlayerController instance yourself (as mentioned in this answer).
Related
Is there a way to add video to a view like you would with an image and an image view? I don't want the full screen mode like in the YouTube app.
You can use AVPlayer from AVFoundation Framework
AVPlayer Demo
Try this code.
1.Import media player framework- #import
2.Declare Instance variable MPMoviePlayerController *player;
-(void)viewDidLoad
{
//you can try other api to get movie file path in ios 8
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Trailer"
ofType:#"m4v"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//—set the size of the movie view and then add it to the View window—
player.view.frame = CGRectMake(10, 10, 300, 300);
[self.view addSubview:player.view];
//—play movie—
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//—called when the movie is done playing—
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
}
//-(NSString *)path
//{
// NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, //NSUserDomainMask, YES);
// NSString *documentDir=[paths objectAtIndex:0];
// return [documentDir stringByAppendingPathComponent:#"Trailer.m4v"];
//}
I'm using MPMoviePlayerViewController to show a video in my app. It works! Only problem is that there's a black flash just before the movie plays.
How can I get rid of the black flash? I've seen other threads, but they don't seem to have an explanation that works with MPMoviePlayerViewController and is sufficiently specific/detailed for a novice like me (most are for MPMoviePlayerController).
Would really appreciate any help!
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"aiw_intro_video" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] init];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mpvc.moviePlayer.controlStyle = MPMovieControlStyleNone;
[mpvc.moviePlayer setContentURL:fileURL];
[mpvc.moviePlayer play];
[self presentViewController:mpvc animated:NO completion:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mpvc.moviePlayer];
After endlessly iterating and tweaking, I stumbled my way into a solution using MPMoviePlayerController.
Don't forget to declare the property in the .h file, e.g.
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
Then
// Add default image to smooth transition
UIImage *myImage = [UIImage imageNamed:#"aiw_launch1136_black.png"];
self.videoStartFrame.image = myImage;
// Play the intro video
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"aiw_intro_video" ofType:#"mp4"]]];
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
[self.moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayer.view];
self.moviePlayer.view.hidden = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(isMovieReady:) name:MPMoviePlayerLoadStateDidChangeNotification object:self.moviePlayer];
// Detect that the video is ready and unhide the view
-(void)isMovieReady:(NSNotification *)notification {
MPMoviePlayerController *moviePlayer = [notification object];
if(moviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK))
{
self.moviePlayer.view.hidden = NO;
}
}
I am trying to play video in iPhone, following is the code i have used.
It keep loading but video doesn't play, am i missing anything.
Player is launched but it sonly shows loading and never play the video.
I have also imported MediaPlayer/MediaPlayer.h
-(IBAction)play:(id)sender
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"test_output" ofType:#"mov"];
NSURL *url = [NSURL URLWithString:
moviePath];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
[_moviePlayer prepareToPlay];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
First, check whether your movie file is added to your project or not.
Second, try creating path with fileURLWithPath. Replace your following code
NSURL *url = [NSURL URLWithString:moviePath];
with
NSURL *url = [NSURL fileURLWithPath:moviePath];
I can't see any code which calls play method of _moviePlayer.moviePlayer.
Try adding following statement to your code.
[_moviePlayer.moviePlayer play];
Add this statement before [self.view addSubview:_moviePlayer.view];
I hope this will work for you
Buddy your code seems to be correct. The problem is definitely with the URL. Double check whether url is correct or not.
here is my code
- (void)viewDidLoad
{
...
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
_moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayerController.controlStyle=MPMovieControlStyleDefault;
[_moviePlayerController play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieIsOver:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)movieIsOver:(NSNotification *)notification
{
NSLog(#"movie is over");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.moviePlayerController.view removeFromSuperview];//moviePlayerController is MPMoviePlayerController
}
When the movie plays to the end, I can't see "movie is over" log and the moviePlayerController.view isn't removed. I don't know why.
EDIT:
MPMoviePlayerPlaybackDidFinishNotification works well.I see the "movie is over" log.The problem is moviePlayerController.view isn't removed.
I found out the solution:add
_moviePlayerController.fullscreen = NO;
before removing view from superview
Instead of adding _moviePlayerController.view as subview
[self.view addSubview:_moviePlayerController.view];
you can present _moviePlayerController just like this :
[self presentMoviePlayerViewControllerAnimated: _moviePlayerController];
and in movieIsOver method you simply dismiss
[self dismissMoviePlayerViewControllerAnimated];
I hope its help
iOS 5.1 ,here is the code:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];
in fact ,this is just the same as the apple reference say,but when I click the button to use this function,there is just a black Rectangle stay there,no vedio playing ,no sound happening,and no crush,so I want to know how to make this work
Sorry for very late reply. The Solution is kind of wierd. But it helped me keep going while facing the same issue.
MPMovieSourceTypeStreaming is the thing you have missed.
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
player.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview: player.view];
// ...
[player play];
You may be sending your
[player play]
message too soon.
Use the following notification observation
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];
To listen for the loadState to change to playable. To check for that update do this :
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
The movie should begin playing once it's ready.
Try this below code for play video from your project Resource :
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"map" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];
Thanks..!
Building on #Dinesh's answer, your URL creation is incorrect. The value of
[NSURL URLWithString:#"map.mp4"]
will be nil since #"map.mp4" is not a valid URL. To create a valid url from the app's bundle do what Dinesh says. To create a remote URL you would do something like
[NSURL URLWithString:#"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"]
The URL should contain all it's parts: protocol, host, path, file.
Cheers, Felipe
Add your movie player controller view on main windows like:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];
[player play];
Hope so it will work!
Your player should be a property of your view controller, something like this:
.h:
#property(nonatomic,weak)MPMoviePlayerController *moviePlayer;
.m:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
//set the player to your property
self.moviePlayer=player;
[self.view addSubview: self.moviePlayer.view];
// ...
[self.moviePlayer play];
Please use MPMoviePlayerViewController Because of MP4 file. When you use MOV then working perfect!!
MPMoviePlayerViewController *moviePlayerViewController;
-(void)PlayVideoController:(NSString*)videoUrl1 {
NSURL *fileURL = [NSURL URLWithString:videoUrl1];
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
//Present
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
// Play the movie!
moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerViewController.moviePlayer play];
}
-(void)myMovieFinishedCallback:(NSNotification*)aNotification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController release], moviePlayerViewController = nil;
}
You might want to cross check video url for blank spaces. Following code works for me.
- (IBAction)btnPlayVideo:(id)sender {
self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#", self.strVideoUrl]];
NSLog(#"Url to play = %#", self.strVideoUrl);
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayerController.view];
self.moviePlayerController.shouldAutoplay = YES;
self.moviePlayerController.fullscreen = YES;
self.moviePlayerController.controlStyle=NO;
[self.moviePlayerController prepareToPlay];
self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;
[self.moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
self.moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
self.closeVideAdProp.hidden = NO;
btnCloseAd.hidden=FALSE;
}