Video Clip does not play correctly - iphone

I am trying to play a video in one of my screens. It seems to run but when the file opens, the fullscreen loads up and closes right after. The video is a few seconds long, and is m4v format. I added the framework and imported the class. How can I make it so that the video plays properly?
My header:
-(IBAction)playMedia:(id)sender {
NSString *movieFile;
MPMoviePlayerController *moviePlayer;
movieFile = [[NSBundle mainBundle]
pathForResource:#"movie" ofType:#"m4v"];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL: [NSURL fileURLWithPath: movieFile]];
[moviePlayer.view setFrame:CGRectMake(145.0, 20.0, 155.0 , 100.0)];
[self.view addSubview:moviePlayer.view ];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMediaFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer play];
if ([toggleFullscreen isOn]) {
[moviePlayer setFullscreen:YES animated:YES];
}
}
My implementation file is:
-(void)playMediaFinished: (NSNotification*)theNotfication {
MPMoviePlayerController *moviePlayer=[theNotfication object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
}
thanks

Related

Playing video in iPad application from server

I want to play a video which is uploaded to the server from an iPad application, but when the screen loads it gives an error:
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
I am using the following code:
-(void)playVideo{
NSURL *url = [NSURL URLWithString:#"http://celeritas.com.pk/emrapp/test.mp4"];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mp];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mp];
[mp release];
NSLog(#"Successfully playing thanks");
}
-(void)playbackFinishedCallback:(NSNotification *)notification{
MPMoviePlayerController *movie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:movie];
[movie release];
}
http://celeritas.com.pk/emrapp/test.mp4 the URL seems to be wrong...
The requested URL /emrapp/test.mp4 was not found on this server.
Any way this will work
.h
MPMoviePlayerViewController * plyr;
NSURL * url;
#property (nonatomic,retain) MPMoviePlayerViewController *plyr;
#property (nonatomic,retain) NSURL *url;
.m
#synthesize plyr ;
#synthesize url;
url = [NSURL URLWithString:#"valid url"];
plyr = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
Url might be wrong double check that video url link is accessible, secondly do see your network firewall this might also cause problem.
The one tweak you can change instead of:
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
use:
mp.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;
try this
create object of MPMoviePlayerViewController in .h like MPMoviePlayerViewController *mp;
#import<MediaPlayer/MediaPlayer.h>
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[self presentMoviePlayerViewControllerAnimated:mp];
try this Code
-(IBAction)playMovie:(id)sender
{
NSURL *fileURL = [NSURL URLWithString:#"http://www.youtube.com/watch?v=3Qjh56woQMw"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotificationobject:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}

streaming videos on iPhone

I want to stream large videos from ftp in iPhone. The video are in size more then 500 MB. I have never done streaming so have no idea about it. I have checked live streaming guide from Apple but it does not provide any help regarding coding in iPhone. Can some one help me what exactly I have to do in iPhone coding? So far I have done following:
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"http://www.defencecourse.com/digital-reproductions/yellow-belt.mp4"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mpvc];
[mpvc release];
Is this coding enough to play a streaming video?
I have a guy who prepare videos for me what should I exactly ask him to do with videos on the server? Should I ask him just to split videos on server or something else?
Can Someone please suggest me best way to forward?
Regards
Pankaj
Check these tutorials AVFoundation Tutorials and read out the Apple's AVFoundation Framework programming guide here
AVFoundation Framework is much more powerful.
Hi You have to do following things on iphone side ...
-(void) btnClose_clicked {
[appDelegate.navShowController dismissModalViewControllerAnimated:YES];
}
-(IBAction) btnPlay_clicked {
// NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
// NSURL *url =[NSURL fileURLWithPath:urlStr];
NSURL *url = [[NSURL alloc] initWithString:[self.DiscAnsDetail objectForKey:#"product_video"]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
// Use the new 3.2 style API
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
} else {
// Use the old 2.0 style API
moviePlayer.movieControlMode = MPMovieControlModeHidden;
[moviePlayer play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// If the moviePlayer.view was added to the view, it needs to be removed
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
}

Play Video In MPMoviePlayerController

I am having issues getting an embedded video file to play using MPMoviePlayerController. My code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *proud = [[documentsDirectoryPath stringByAppendingPathComponent:#"archives"] stringByAppendingPathComponent:selectedCountry];
NSString *content = [proud stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL fileURLWithPath:content] retain];
NSLog(#"%#", url);
player =
[[MPMoviePlayerController alloc] initWithContentURL: url];
[player prepareToPlay];
player.allowsAirPlay = YES;
player.scalingMode = MPMovieScalingModeAspectFit;
player.view.frame = self.view.frame;
[self.view addSubview: player.view];
[player setFullscreen:YES animated:YES];
// ...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerWillExitFullscreen:)
name:MPMoviePlayerWillExitFullscreenNotification
object:player];
[player play];
The NSLog returns
var/mobile/Applications/EF62B00B-2906-435C-BC84-036FE14D89E9/Documents/archives/Test%2520-%2520Daddy%2520May%25201%252001:26:35%2520PM.mp4
But the video never plays, though MovieDone does get triggered from the log I use in MPMoviePlayerController MovieFinished Callback. Any thoughts?
i think you play only video file then try this code :
NSString *urlStr=[[NSBundle mainBundle]pathForResource:#"Video1VoiceOver.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(174,154,720,428);
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
Try this
MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:#"FileName"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:)name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
playerViewController.view.frame=CGRectMake(Set the frame);
[self.view addSubview:playerViewController.view];
//---play movie---
player = [playerViewController moviePlayer];

Video won't play on iPhone 5.0 simulator

Im new to xCode programming, ive got this code from an ebook tutorial playing mp4 xCode.
function triggered from a button
(IBAction)playMovie:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"videoSample" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen=YES;
[moviePlayerController play];
moviePlayerController.scalingMode = MPMovieScalingModeFill;
}
called from play movie function
(void)moviePlaybackComplete:(NSNotification *)notification{
MPMoviePlayerController *moviePlayerController = [notification
object];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
}
Ive got no error and warning after i build it. it just output a blank screen after i click the button that triggers the playMovie function. Im confuse ive google it and still got no idea on how to solve the proble.
Im using xCode 4.2 iOS SDK 5.0
try this....replace "example" with the name of your file and "m4v" with the type of file e.g. "mp4" (not .mp4 and dont put the .mp4 bit in the name of the file)
-(IBAction)playVideo:(id)sender;
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"example" ofType:#"m4v"];
MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
if (tmpMoviePlayViewController) {
tmpMoviePlayViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tmpMoviePlayViewController animated:YES];
tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieViewFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:tmpMoviePlayViewController];
[tmpMoviePlayViewController.moviePlayer play];
}
}
-(void)myMovieFinishedCallback:(NSNotification*)theNotification
{
MPMoviePlayerController *moviePlayer=[theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
}
I don't know if you are still looking for the answer. Here's what you can try - declare the MPMoviePlayerController in your header(.h) file (i.e. make it a member variable).
MPMoviePlayerController *moviePlayerController;
and add this to your .m file
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
If it still does not work then try to declare it as a #property.

How to change view after video played?

I would like to make an application in which, when I press a button, one video starts playing, and when it finishes or when I press "done" button it should take me to a view different from the first one where I launched the video.
Update:
This is the code I'm using but it doesn't work. I need to use MPMoviePlayerViewController instead of MPMoviePlayerController. Any idea?
NSBundle *Bundle = [NSBundle mainBundle];
NSString *moviePath = [Bundle pathForResource:#"video1" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
- (void) movieFinishedCallback:(NSNotification*) notification {
MPMoviePlayerViewController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer release];
// dismiss your view or present a new view here.
View1 *View1b = [[View1 alloc] initWithNibName:nil bundle:nil];
View1b.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: View1b animated:YES];
}
Unless you need something very custom, MPMoviePlayerController will probably suit your needs. You can add it to your view or a smaller subview, and you can disable the controls for fullscreen, etc. The url can be to a local file or remove resource.
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player view].frame = [myView bounds];
[myView addSubview: [player view]];
[player play];
Observe the MPMoviePlayerPlaybackDidFinishNotification to figure out when it is done, and from that observer's block or selected method you can dismiss your view or present a new view.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
Then
- (void) movieFinishedCallback:(NSNotification*) notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player release];
// dismiss your view or present a new view here.
}