Video doesn't show up on screen - iphone

I imported the MPMoviePlayerController in my VideosView.h.
In my VideosView.m i embed the following code:
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mp4" inDirectory:#"images"];
NSLog(#"%#", path2);
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path2];
myPlayer.view.frame = CGRectMake(0, 0, 500, 500);
myPlayer.scalingMode = MPMovieScalingModeFill;
myPlayer.controlStyle = MPMovieControlModeDefault;
[self addSubview:myPlayer.view];
[myPlayer play];
I've found this example on Stackoverflow, but can't get it working. The link to my video is correct (yes it's in the images folder). I get a 500 by 500px black rectangle on my screen (the frame ofcourse) but no video is playing.
Some help would be great.
W.

Define myPlayer object globally
in your code, the life of the myPlayer ends with the scope of the variable. If you have created inside a method. The player ends with the scope of that method.
#property(nonatomic, strong) MPMoviePlayerController *myPlayer;
then init from anywhere you want,
_myPlayer = [[MPMoviePlayerController alloc] init];

self.moviePlayerView = [[MPMoviePlayerViewController alloc]initWithContentURL:videoURL];
movie = [self.moviePlayerView moviePlayer];
movie.controlStyle = MPMovieControlStyleNone;
[movie setControlStyle:MPMovieControlStyleFullscreen];
self.moviePlayerView.moviePlayer.shouldAutoplay=YES;
[movie prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateChanged)
name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
self.moviePlayerView.view.frame = CGRectMake(0.0f, 0.0f, 304.0f, 221.0f);
[[self.moviePlayerView moviePlayer]play];
add bewlow..
- (void) playbackStateChanged
{
self.moviePlayerView.moviePlayer.shouldAutoplay=YES;
MPMoviePlaybackState playbackState = [self.moviePlayerView.moviePlayer playbackState];
switch (playbackState)
{
case MPMoviePlaybackStateStopped :
break;
case MPMoviePlaybackStatePlaying :
break;
case MPMoviePlaybackStateInterrupted :
break;
}
}

Related

repeatmode doesn't work in MPMoviePlayerViewController?

How to display a recorded video as repeat as an'n' number of times like in Vine Application.
Here I use the MPMoviePlayerViewController, and works great display the recorded video. But the problem is, it doesn't repeating.
Here the currently using code is,
NSURL *url = [NSURL fileURLWithPath:videoPath];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer prepareToPlay];
playerController.view.frame = CGRectMake(200, 402, 300, 200);
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview: playerController.view];
[playerController.moviePlayer play];
NSLog(#"repeatMode: %d",playerController.moviePlayer.repeatMode);
[playerController.view addSubview:customview];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerController moviePlayer]];
And the NSNotification code,
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
NSLog( #"myMovieFinishedCallback: %#", aNotification );
MPMoviePlayerController *movieController = aNotification.object;
NSLog( #"player.playbackState = %d", movieController.playbackState );
}
Can anyone please give the solution..
Note:
I'm using the XCode 4.5.2 tool and ios simulater 6.0
Try this code. It is working.
NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Video" ofType:#"mp4"]];
MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl];
[moviePlayerController.moviePlayer prepareToPlay];
[moviePlayerController.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
[moviePlayerController.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
[self.view addSubview:moviePlayerController.view];
you can try this code
-(void) movieFinishedCallback:(NSNotification *) aNotification
{
if (aNotification.object == self.moviePlayer) {
NSInteger reason = [[aNotification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[playerController play];
}
}
}
You have to keep the MPMoviePlayerController object as a member, otherwise the app would lose context to it and the movie will not loop.

i have videos list when i select row video will play on another view how to stop and come back for table view

if (indexPath.row==0)
{
NSString *urlAsString = [[NSBundle mainBundle] pathForResource: [self.array objectAtIndex:indexPath.row] ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:urlAsString];
[self.view addSubview:myPlayer.view];
[myPlayer.view setFrame:CGRectMake(0, 0, 320, 400)];
[myPlayer setFullscreen:YES animated:YES];
[myPlayer play];
}
i have one button in MPMoviePlayerController called done when i click on done button i need to come back to my tableview plz help me any one
MPMoviePlayerController *myPlayer declare global and init . and
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:nil];
And selector method:
-(void)doneButtonClick:(NSNotification*)aNotification{
[myPlayer stop];
}

MPMoviePlayerController loadState doesn't switch to MPMovieLoadStatePlayable

I have a MPMoviePlayerViewController to play Movies in fullscreen. I checked the movies they play fine via Quicktime.
The Problem is, that using the MPMoviePlayerViewController (Simulator and device) the Movie doesn't start playing(it's a video that is stored locally on the iPad btw).
NSString *path = [[NSBundle mainBundle]pathForResource:resource ofType:#"mov"];
self.mpviewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:path]];
[self.mpviewController.moviePlayer prepareToPlay];
self.mpviewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.mpviewController.moviePlayer.controlStyle = [self presentModalViewController:self.mpviewController animated:YES];
NSLog(#"%d",self.mpviewController.moviePlayer.loadState);
[self.mpviewController.moviePlayer play];
Any Ideas what I am missing ?
tia
well, I dont know what it was. I ended up writing a new view controller with this code - that worked:
NSURL *url = [NSURL fileURLWithPath:self.urlPath];
self.playerController = [[MPMoviePlayerController alloc] initWithContentURL: url];
CGRect bounds = CGRectMake(0, 0, 1024, 748);
self.playerController.scalingMode = MPMovieScalingModeNone;
[self.playerController.view setFrame:bounds];
[self.view addSubview:self.playerController.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidExitFullscreenCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerController];
self.playerController.controlStyle = MPMovieControlStyleFullscreen;
[self.playerController setFullscreen:YES animated:YES];
[self.playerController play];

IOS [NSURL initFileURLWithPath:]: nil string parameter on loading video

I have a problem with my video content. I want to load a video from my viewcontroller and I get this error:
[NSURL initFileURLWithPath:]: nil string parameter
When I try to load the video from a url the video wont show up.
I am sure that my variable targetURN is not nil.
From here I load my viewcontroller:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
[self openVideoView:#"Video"];
break;
case 1:
[self openWebview:#"About"];
break;
case 2:
[self openWebview:#"http://www.url.com"];
break;
}
}
- (void)openVideoView:(NSString *)url{
IDPVideoView *vv = [[IDPVideoView alloc] initWithURN:url];
[self presentModalViewController:vv animated:NO];
}
In my viewcontroller:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *videoURL = nil;
if ([self.targetURN hasPrefix:#"http"])
{
videoURL = [NSURL URLWithString:targetURN];
}
else
{
videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:targetURN ofType:#"mp4"]];
}
if(videoURL != nil) {
MPMoviePlayerController *moviePlayer =
[[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[moviePlayer prepareToPlay];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
}
have you tried allocating the url first?
NSURL *vidURL = [[NSURL alloc] initWithString:self.targetURN];
as a general advice, try using self.targetURN all the time, or synchronize it to a different name like
#synchronize targetURN = _targetURN
and then use _targetURN in your code.this makes sure that you (in any case) use the right property.
(of course this goes for all your properties)
I solved the error part by adding this line
NSString *urlString = [NSString stringWithFormat:url];
- (void)openVideoView:(NSString *)url{
NSString *urlString = [NSString stringWithFormat:url];
IDPVideoView *vv = [[IDPVideoView alloc] initWithURN:url];
[self presentModalViewController:vv animated:NO];
}
But the video is still not showing up.

Play Video Url in iphone

How To play Server URL Video in Iphone ?
how to play video from url using MPMoviePlayer ?
(void)loadVideo {
videoUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://movies.apple.com/media/us/mac/getamac/2009/apple-mvp-biohazard_suit-us-20090419_480x272.mov"]];
if(videoUrl) {
if([videoUrl scheme]) {
[self playVideoStream:videoUrl];
}
}
}
-(void)playVideoStream:(NSURL *)movieFileURL {
MPMovieSourceType movieSourceType = MPMovieSourceTypeUnknown;
/* If we have a streaming url then specify the movie source type. */
if ([[movieFileURL pathExtension] compare:#"mov" options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
movieSourceType = MPMovieSourceTypeStreaming;
}
[self createAndPlayMovieForURL:movieFileURL sourceType:movieSourceType];
}
-(void)createAndPlayMovieForURL:(NSURL *)movieURL sourceType:(MPMovieSourceType)sourceType {
/* Play the video! */
[moviePlayer setMovieSourceType:sourceType];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];;
moviePlayer.view.frame = CGRectMake(10, 20, 280, 210);
[customVideoView addSubview:moviePlayer.view];
[moviePlayer play];
}
You need to set contentURL property at fisrt
moviePlayer.contentURL = urlVideo;
[moviePlayer prepareToPlay];
[moviePlayer play];
try this:
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"//name of your video here" ofType:#"//format e.g. mp4, m4v"]];
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
// When movie finished loading you can have a notification and do extra code
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MovieDidLoad:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[playerController release];
playerController = nil;
}
-(void)MovieDidLoad:(NSNotification *)notification {
NSLog(#"logged and notification is %#", notification);
// extra code if needed
}
One of the easy way is to load that video url into a UIWebview.