MPMoviePlayerController not working on iOS8 - iphone

I was trying out a project I worked on during iOS6. Tried running the same on iOS8 and the video is playing. Is there anything else I need to do?
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Mcdonald" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayerController];
}
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
Thanks.

Finally. Adding it to the UIWindow solved the problem for me.
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"disc" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];

Init your MPMoviePlayerController in .h file like below :
#property(strong,nonatomic)MPMoviePlayerController *moviePlayerController;
After above changes it will work .
Sample Code:
#interface ViewController : UIViewController
#property(strong,nonatomic)MPMoviePlayerController *moviePlayerController;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Mcdonald" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
_moviePlayerController.allowsAirPlay = NO;
_moviePlayerController.shouldAutoplay = YES;
[_moviePlayerController.view setFrame:[UIScreen mainScreen].bounds];
_moviePlayerController.controlStyle=MPMovieControlStyleEmbedded;
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayerController];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Related

MPMoviePlayerController is not playing video

I am using image matcher sdk with MPMoviePlayerController to play video after matched.
In NSLog video is process but not play.
Here .m file`
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize moviePlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
_cvView = [[ImageMatcher alloc] initWithAppKey:#"apikey" useDefaultCamera:TRUE];
_cvView.matcherDelegate = self;
[_cvView start];
[_cvView setEnableMedianFilter:YES];
[_cvView setImagePoolMinimumRating:0];
[_cvView setMatchMode:matcher_mode_All];
bool value = [_cvView addImage:[UIImage imageNamed:#"pic2.jpeg"] withUniqeID:[NSNumber numberWithInt:20]];
}
-(void) viewDidAppear:(BOOL)animated
{
[self presentViewController:_cvView animated:YES completion:nil];
}
-(void) imageRecognitionResult:(int)uId
{
if (uId == 20) {
[self startPlayingVideo2:nil];
NSLog(#"ID = %d",uId);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void) startPlayingVideo1:(id)paramSender{
NSLog(#"function startPlayingVideo make");
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *urlAsString = [mainBundle pathForResource:#"test1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
if(self.moviePlayer != nil){
NSLog(#"if 1 make");
[self stopPlayingVideo:nil];
}
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
if(self.moviePlayer != nil){
NSLog(#"if 2 make");
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 760);
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}else{
NSLog(#"fail");
}
}
-(void) startPlayingVideo2:(id)paramSender{
NSLog(#"function startPlayingVideo make");
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *urlAsString = [mainBundle pathForResource:#"sample_mpeg4" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
if(self.moviePlayer != nil){
NSLog(#"if 1 make");
[self stopPlayingVideo:nil];
}
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
if(self.moviePlayer != nil){
NSLog(#"if 2 make");
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 760);
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}else{
NSLog(#"fail");
}
}
-(void) stopPlayingVideo:(id)paramSender{
if (self.moviePlayer != nil) {
NSLog(#"stopPlayingVideo on");
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[self.moviePlayer stop];
if([self.moviePlayer.view.superview isEqual:self.view]){
[self.moviePlayer.view removeFromSuperview];
}
}
}
-(void) videoHasFinishedPlaying:(NSNotification *)paramNotification{
NSNumber *reason = [paramNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if (reason != nil) {
NSInteger reasonAsInteger = [reason integerValue];
switch (reasonAsInteger){
case MPMovieFinishReasonPlaybackEnded:{
NSLog(#"MPMovieFinishReasonPlaybackEnded");
break;
}
case MPMovieFinishReasonPlaybackError:{
NSLog(#"MPMovieFinishReasonPlaybackError");
break;
}
case MPMovieFinishReasonUserExited:{
NSLog(#"MPMovieFinishReasonUserExited");
break;
}
}
NSLog(#"finish reason = %ld", (long)reasonAsInteger);
[self stopPlayingVideo:nil];
}
}
#end
`
Add prepareToPlay in your code (Prepares a movie player for playback. (required))'
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
To prepare a new movie player for playback, call the prepareToPlay method, described in MPMediaPlayback Protocol Reference.

How to record video of app screen

I have created a streaming video app. and now i want to record that video which is playing in MPVideoPlayer,also its sound. I have tried this code. but this code is only record black screen and no sound. I am adding MPVideoPlayer instance dynamically. so , plz help me to do this any source code or library will be great help.
Try this :
.h file
import MediaPlayer/MediaPlayer.h
NSString *strSelectedVideoPath;
MPMoviePlayerController *player
#property (nonatomic,retain) NSString *strSelectedVideoPath;
#property (nonatomic,retain) MPMoviePlayerController *player;
.m file
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:strSelectedVideoPath]];
player.scalingMode = MPMovieScalingModeFill;
player.movieSourceType = MPMovieSourceTypeFile;
player.view.frame = CGRectMake(0, 45, 320, 400);
player.shouldAutoplay = YES;
[player prepareToPlay];
[self.view addSubview:player.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player1 = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player1];
[player1.view removeFromSuperview];
player1 = nil;
}
You have to add MediaPlayer.framework
For Record video :
-(void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey: UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath,self,#selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
if (error) {
}
else{
NSURL *videoURl = [NSURL fileURLWithPath:videoPath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
//For Thumb Image
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
self.strUploadVideoPath = videoPath;
CGImageRelease(imgRef);
[generate release];
[asset release];
}
}

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.

iPhone SDK: Stopping a Video

How can I stop a video once I navigate away from a view and make the video play again when I get back to the original view again?
Here is the code to play the video in the original view:
- (void)viewDidLoad {
NSBundle *bundle=[NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Video" ofType:#"mp4"];
NSURL *movieURL=[[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
theMovie.view.frame = CGRectMake(104.0, 134.0, 200.0, 250.0);
[self.view addSubview:theMovie.view];
[theMovie play];
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
}
Make theMovie instance var
Stop playing in the -(void) viewWillDisappear:(BOOL)animated
And it should start playing when you navigate back to this view. Or move this code to -(void) viewDidAppear:(BOOL)animated

i have a problem with MPMoviePlayerController [iPhone SDK]

i want create an app with movie intro just like gameloft games . so when application has lunched , movie plays fine but before the move plays .. my FirstViewController xib file show first then movie start to play ! why ? here is my code :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"movie" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
IntroMovie.movieControlMode = MPMovieControlModeHidden;
[IntroMovie play];
}
You need to wait for the movie to finish playing before adding your first view as a subview to window.
This should do the trick. Change your code to:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Hafez-2" ofType:#"mov"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[IntroMovie setOrientation:UIDeviceOrientationPortrait animated:NO];
[IntroMovie play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
An alternative is to add a different view (e.g. simple black background) and then replace it when the video has finished playing:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Hafez-2" ofType:#"mov"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[IntroMovie setOrientation:UIDeviceOrientationPortrait animated:NO];
[IntroMovie play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// Create an initial pure black view
UIView* blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
blackView.backgroundColor = [UIColor blackColor];
[window addSubview:blackView]; // sends [blackView retain]
[blackView release];
[window makeKeyAndVisible];
}
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
UIView* blackView = [[window subviews] objectAtIndex:0];
[blackView removeFromSuperview]; // sends [blackView release]
[window addSubview:viewController.view];
}
Let me know if you need modified source from your example and I can post it somewhere