Ive been battling with a crash on my exercise app for the past couple of days when I try and play an instance of an MPMoviePlayerController in a UIView created in IB.
The rest of the app runs fine, but if any MPMoviePlayer is instantiated, the debugger pauses the app without raising an exception. It's not just this code that causes it. Other methods of instantiating a moviePlayer from other posts or books cause the same result.
Its when I run this code that it drops out to the main:
NSURL *url = exercise.exerciseVideoURL;//path for vid
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayerController.view setFrame:self.movieHostingView.bounds];
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
[self.movieHostingView addSubview:self.moviePlayerController.view];
[[self.moviePlayerController view]
setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight)];
[[self.moviePlayerController view] setClipsToBounds:YES];
[[self.moviePlayerController view] setFrame:[[self movieHostingView] bounds]];
[self.movieHostingView addSubview:self.moviePlayerController.view];
self.moviePlayerController.repeatMode = MPMovieRepeatModeOne;
I've tried using MPMoviePlayer instantiating code from other posts, binning off the model and just instantiate it with a direct path like in some of the other posts, but it still drops out to main().
Got it! Ive removed "All Exceptions" from the breakpoint tab and its running fine.Why would that cause it to drop out for MPMoviePlayer?!
Anyhow, Thanks to everyone who helped out.
are you reading video from disk (main bundle) or via internet?
First, be always sure that your filepath is correct:
NSString *filepath = [url absoluteString];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
NSLog(#"file exists so init player");
}
else
{
NSLog(#"try again!");
}
In my case I always add MPMoviePlayerController after its loaded.
So you can do something like this:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"mp4"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[_moviePlayerController prepareToPlay];
NSLog(#"frame = %#", NSStringFromCGRect(_moviePlayerController.backgroundView.frame));
[_moviePlayerController.view setBackgroundColor:[UIColor yellowColor]];
[_moviePlayerController.view setFrame:CGRectMake(0, 280, 320, 200)];
//
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieLoadStateDidChangeNotification:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayerController];
}
and then some time later:
- (void)movieLoadStateDidChangeNotification:(NSNotification *)notification
{
[self.view addSubview:_moviePlayerController.view];
}
Related
NSURL *url="file:///Users/adi/Library/Developer/CoreSimulator/Devices/F7C9D729-6E67-45C3-9234-629460FDD0A4/data/Containers/Data/Application/319F0627-CF3E-43D4-A44F-3E5E797CCCEA/Library/Caches/Attachments/7157783VID_20150602_185133.mp4"
Here's my code:
MPMoviePlayerController * moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(10, 100, 300, 200);
[moviePlayer play];
It shows blank screen.
Hope this may help you
#import<MediaPlayer/MediaPlayer.h>
-(IBAction)VideoPlayer:(id)sender
{
NSString *path = [[NSBundle mainBundle]pathForResource: #"video-en-2b" ofType:#"mp4"];
moviePlayer = [[MPMoviePlayerViewControlle alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
Codin[self presentModalViewController:moviePlayer animated:NO];
}
Well if you have to play video using local file use this code.It is helpful for you.
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"yourVideo" ofType:#"mp4"];//7157783VID_20150602_185133 name
NSURL*theurl=[NSURL fileURLWithPath:thePath];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[moviePlayer.view setFrame:CGRectMake(10, 100, 200, 300)];
[moviePlayer prepareToPlay];
[moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
[self.view addSubview:moviePlayer.view];
If you want to control playback process
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
Try this :
-(void)playVideo : (NSString*)apath
{
NSURL *videoURL =[NSURL fileURLWithPath:apath];
if ([[NSFileManager defaultManager] fileExistsAtPath:apath]) //Does file exist?
{
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
videoPlayerView.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
}
Note: this method only accept local file path
Does anyone know how I can use a background video as the login like how AirBNB had their log in screen before? I tried to use a gif, but felt like it didn't have great resolution.
Please let me know! thank you!
Have not tried it myself but it should at least send you to the right direction:
- (void)viewDidLoad
{
[super viewDidLoad]
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"your_video_file_name" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
// or get your videoURL some another way
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer.view setFrame:self.view.bounds];
[self.view insertSubview:moviePlayer.view atIndex:0]; // to be sure it is a background
// [self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
I'm developing an app for iOS where it shows a movie, my method is a class method this is the code
Functions.m
static MPMoviePlayerController *moviePlayer = nil;
+(void)playMP4Movie:(NSString *)moviename
insideOfView:(UIView *)view
autoplay:(BOOL)autoplay{
NSString *path = [[NSBundle mainBundle] pathForResource:moviename ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayer.repeatMode = MPMovieRepeatModeOne; // for looping
moviePlayer.view.frame = view.bounds;
[moviePlayer prepareToPlay];
if (autoplay) { [moviePlayer play]; }
else { [moviePlayer stop]; }
[view addSubview:moviePlayer.view];
}
and where I want to call it with video paused:
[Functions playMP4Movie:videoName insideOfView:_videoPlayerView autoplay:NO];
it shows my video but no matter if I set autoplay to YES or NO, the result is always the same... any help I'll appreciate
Try using:
if (autoplay) { [moviePlayer setShouldAutoplay:YES]; }
else { [moviePlayer setShouldAutoplay:NO]; }
And see if you have better luck.
I am trying to change the content of my MPMoviePlayerController method with function running in background.But my content is not getting changed.
[self performSelectorInBackground:#selector(updateVideo) withObject:nil];
This is my viewDidLoad method where I start loading a video.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VideoView *videoView = [[VideoView alloc] initWithFrame:CGRectMake(0, 0, 293, 320)];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"My Movie4" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
controller = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[controller.view setFrame:videoView.bounds];
[videoView addSubview:controller.view];
[self.videoviewer addSubview: videoView];
[controller prepareToPlay];
[controller play];
}
Where VideoView is a subclass of UIView.My updateVideo function is like this.
-(void)updateVideo
{
[controller setContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"video2" ofType:#"mp4"] ]];
[controller prepareToPlay];
[controller play];
}
But the video content is not getting changed when the function is called which is running in background.
Why are you trying to change the content in the background thread ?
You can't change UI element in the background thread. You have to execute this task in the main thread.
Try to do this :
[self updateVideo]
Or this if you want, but I recommend you to use the first method.
-(void)updateVideo
{
[controller setContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"video2" ofType:#"mp4"] ]];
dispatch_async(dispatch_get_main_queue(), ^{
[controller prepareToPlay];
[controller play];
});
}
I hope this can help you
i am using this code to playback a video on ios 6 (in Xcode and device simulator)
- (void) playMovie {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"flying" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController prepareToPlay];
[moviePlayerController.view setFrame: self.view.bounds];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerController play];
}
playback starts, but the video aborts after 5 seconds. It simply disappears and i see a black window and no error messages in Xcode.
any clues?
Thanks
The MPMoviePlayerController variable is being released after 5 seconds. I introduced the variable declaration at the implementation level and the definition in the playMovie method. Now it works!