Can not play video from URL in iPhone app? - objective-c

I'm having a problem playing the video from URL-based. This is my code:
-(void)clickplay
{
NSURL *fileURL = [NSURL URLWithString:#"http://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1211/sample_iTunes.mov"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc]initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
It compiles successfully and the player did pop out but it just keeps loading forever... I have tried changing the URL several times to no effect. Can anyone please help me to solve this problem?

You'll need to retain a reference to the moviePlayerController. It's getting released and preventing any delegated messages from being invoked. Probably the easiest place to put it is in the class that contains the - (void)clickplay implementation. That is, add a new instance variable called moviePlayerController and assign it the newly created MPMoviePlayerController object in clickplay.

videoURL=[NSURL URLWithString:URL]];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
videoPlayer.view.frame = CGRectMake(0,0, 320, 480);
[videoPlayer prepareToPlay];
[self.view addSubview:videoPlayer.view];
[videoPlayer play];
videoPlayer.movieControlMode = MPMovieControlModeHidden;
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayer];
/* configuring notification*/
- (void)moviePlayBackComplete:(NSNotification *)notification
{
[videoPlayer stop];
}

This my answer in regards with playing .3gp file with iPhone .....I have successfully played .3gp file with iphone sdk with using MpMoviePlayerController and my answer link is:
Link: https://stackoverflow.com/a/13088808/1092219
hope you will got help from my answer.........!! :))))))

Related

Play video through URL link

I'm trying to play a youtube video through my app to test for videos that will need linked later (no videos to link yet). I'm trying everything from a UIImageView to UIWebView using MPMoviePlayer. It starts the player, then stops seconds later with the following in the log:
_itemFailedToPlayToEnd:
{
kind = 1;
new = 2;
old = 0;
}
_itemFailedToPlayToEnd:(null)
I'm not sure what's going wrong as I've not that much experience with streaming videos into my apps. Thanks for the help.
-(void)playMovie:(id)sender
{
UIWebView *video1 = [UIWebView new];
video1.frame = CGRectMake(100, 100, 60, 60);
NSURL *url = [NSURL URLWithString:#"https://www.youtube.com/watch?v=w9bBQRWkV8M"];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(movieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mp];
[self.view addSubview:self.player.view];
[self.player setFullscreen:YES];
self.player.shouldAutoplay = YES;
[self.player prepareToPlay];
}

MPMoviePlayerController and let the user replay

I have an iOS app with a MPMoviePlayerController, I need to play a video from an URL. Everything seems to work fine, but when the playback ends the MPMoviePlayerController shows a weird image and it takes AGES to replay the video...
This is what I have so far:
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
And the moviePlayerLoadStateChanged and moviePlayBackDidFinish look like this:
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[mp setControlStyle:MPMovieControlStyleEmbedded];
[mp setFullscreen:NO];
[mp.view setFrame:CGRectMake(10, 54, 300, 200)];
[mp setShouldAutoplay:NO];
[mp prepareToPlay];
[[self view] addSubview:[self.mp view]];
}
}
I don't know what that Image is, but I would like to replace it... also I think it takes so much because it's loading the video from a URL, I don't know how to add a loading, or a spinner... Any idea?
You could set the repeatMode property of your MPMoviePlayerController to MPMovieRepeatModeOne
by using:
mp = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];///Put your path to your resource
mp.moviePlayer.repeatMode = MPMovieRepeatModeOne;
I hope this helps if i understood your question correctly.
If not i recommend checking out the documentation HERE
Happy coding:)

play video inside frame iOS (and center it)

I don't want to play a video in fullscreen, is it possible to play a video inside a view?
Here is my code. It does create a frame and plays the video inside it. The problem is that I don't know how to center on screen the frame (it appears on the right side of the screen)
Screenshot
NSBundle *bundle = [NSBundle mainBundle];
NSURL *url = [NSURL fileURLWithPath:[bundle pathForResource:#"movie" ofType:#"mov"]];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
_moviePlayer.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:_moviePlayer.view];
_moviePlayer.shouldAutoplay = YES;
replace a line with given below will fix your issue.
_moviePlayer.view.center = self.view.center;

disable user interaction in MPMoviePlayerController

I need to disable total user interaction during a play of a litle video, i already tried to set the style of the control to none : MPMovieControlStyleNone, and put a UIView above the MPMoviePlayerController, but when i pinch the video disappears, and the sound still playing, like i dismissed the video, but it still playing on the background, plus the user interaction is disabled.
Heres how i do:
-(IBAction)startGame:(id)sender
{
NSURL * url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Teste" ofType:#"m4v"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
mBlankView = [[UIView alloc] initWithFrame:moviePlayer.view.frame];
mBlankView.userInteractionEnabled = NO;
[mBlankView setMultipleTouchEnabled:NO];
[mBlankView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:mBlankView];
}
To disable Movieplayer control completely u need to disable your movieplayers view userinteraction as follows:
moviePlayer.view.userInteractionEnabled = NO;

How can I know when video play?

I have video in my project. I play it with MPMoviePlayerController but app will take time to load for a sec. Now I want to lunch another func. when video start to play. How can I know when video already load and start play and How to call another func. when it play ???
Could U guide me please ?
here is some simple code
-(IBAction)testVideoLongPress:(id)sender
{
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"video1" ofType:#"MOV"];
movie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
movie.view.backgroundColor = [UIColor blackColor];
movie.controlStyle = MPMovieControlStyleDefault;
movie.shouldAutoplay = YES;
movie.view.frame = CGRectMake(0, 50, 720, 1280);
[self.view addSubview:movie.view];
[movie play];
}
-(void)wannalunchthismethod
{
....
}
Look at the MPMoviePlayerLoadStateDidChangeNotification notification of MPMoviePlayerController and you'll be able to catch your movie player state changing to "play".