Play background video in iphone App - ios

I'm developing a iPhone app and need to play the animation video in the background. This video should run until the user touch's the screen of the app. Not exactly the screensaver but this should happen for the first time when the app is opened.
How can play the video file in the background and screen buttons on top of the video in the app?

Add video player in your view of subview and your view must be a transparent layer makes.
your app delegate check the user coming first time or not.
NSString *path = [[NSBundle mainBundle] pathForResource:#"testVideo" ofType:#"mp4"];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path];
[self.view addSubview:myPlayer.view];
[myPlayer play];

Simply add MPMoviePlayerController as background and play the video,
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: video_url];
[player prepareToPlay];
[player.view setFrame: requiredFrame];
[self.view addSubview: player.view];
After that, add tap gesture to whole view and remove player on tap gesture method..

Related

MPMoviePlayerController plays without -play being called after -prepareToPlay is called

I have some code that sets up an MPMoviePlayerController to play back a video stored in the app.
I follow the example code in Apple's documentation. However, the video plays even if I don't call -play on it as long as I have called -prepareToPlay.
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_videoName ofType:#"mp4"]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[player setMovieSourceType:MPMovieSourceTypeFile];
[player setScalingMode:MPMovieScalingModeAspectFit];
[[player view] setFrame:[[self view] bounds]];
[[self view] addSubview:[player view]];
[player prepareToPlay];
//[player play];
Will play the video. I uncomment the -play and the same thing happens. It does not matter if I have -play in or not. And I had -prepareToPlay earlier in the code as well (before the view setup) and it did not make a difference.
This is on iOS8 of some sort. I have not tried other versions of iOS.
Why does it work like this? The Apple docs make it sound like it should not play until -play is called.
Try adding this line of code
player.shouldAutoplay = NO;
By default it is set to YES.

Loading video when clicking forward or Backward video in MPMoviePlayerController-iOS 7

It will showing black screen with loading activity indicator When i click the video in full screen and touch up the forward or Backward button.
I have using the following code for movie player
NSURL *fURL = [NSURL URLWithString:[self.winnersCircleDictionary objectForKey:#"videoname"]];
self.players=nil;
self.players=[[MPMoviePlayerController alloc]initWithContentURL:fURL];
[self.players setContentURL:fURL];
self.players.scalingMode = MPMovieScalingModeAspectFit;
self.players.repeatMode=MPMovieRepeatModeOne;
[[self.players view] setFrame:_webviewWinnerCircle.frame];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.players];
object:nil];
self.players.shouldAutoplay=NO;
self.players.currentPlaybackTime=0.0;
[scrollViewWinnerCircle addSubview:self.players.view];
Forward and Backward button working fine in iOS 6 and this issue only have in iOS 7.
Thank You.
I just tried your code and got the issue , And solved like this.
Prepared the MoviePlayer like [self.players prepareToPlay]; for playing the selected video after adding the self.player.view as subview to the scrollViewWinnerCircle.
The code is as follows.
self.players = [[MPMoviePlayerController alloc]initWithContentURL:fURL];
[self.players setContentURL:fURL];
self.players.scalingMode = MPMovieScalingModeAspectFit;
self.players.repeatMode = MPMovieRepeatModeOne;
[[self.players view] setFrame:_containerView.frame];
self.players.shouldAutoplay = NO;
self.players.currentPlaybackTime = 0.0;
[_containerView addSubview:self.players.view];
[self.players prepareToPlay]; // Prepares a movie player for playback.
[self.players pause]; // Pause playback of the loaded movie
It was the problem with the source, also didn't specified the source type.
Do like the following, its working for me with the url i used here.
NSURL *fURL = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.players = nil;
self.players = [[MPMoviePlayerController alloc]initWithContentURL:fURL];
[self.players setContentURL:fURL];
// Specifying media source type
self.players.movieSourceType = MPMovieSourceTypeStreaming;
It take some time to load initially.
you have to use this code instead of MPMovieplayerController and import these library in you class:
AVKit
AVFoundation
NSURL *videoURL = [NSURL URLWithString:#"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[player play];
[self presentViewController:playerViewController animated:YES completion:nil];
after using above code you will be able to forward and back forward video.

How to play a video randomly without pressing the play button

I'm new in iPhone Application Development, and now i faced a problem, can any one tell me how can i play a video randomly without pressing the play button. The video should start automatically while the application launch and play randomly without stopping. I don't have any idea how to do this. I don't have any code also. Please help me any one...
Thanks in advanced.
You could implement it like this:
You need a movie player configured with e.g. a file url and add it to your view
NSURL *url = [[NSBundle mainBundle] URLForResource:movieName withExtension:#"mov"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = // set the frame
[self.view addSubview:moviePlayer.view];
Start the movie player
[moviePlayer play];
Listen to the notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleMoviePlayerStatChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
Start the video again
- (void)handleMoviePlayerStatChange:(NSNotification *)note {
NSLog(#"mp playback state %i", moviePlayer.playbackState);
if (moviePlayer.playbackState == MPMoviePlaybackStatePaused) {
[moviePlayer play];
}
}
To hide the video controls set the controlStyle property of the movie player instance:
moviePlayer.controlStyle = MPMovieControlStyleNone;

When video stops it goes to black screen, I need it to go to an image or pause image it was last on

I have multiple videos on one view, when one video has finished playing, the user taps a button and the next video starts to play.
The video that has just played goes to a black screen.
I want the video to stop (pause) where it is, so the image is still there.
this is my code;
}
-(IBAction)play {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"origami_panel01" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playercontroller.moviePlayer.controlStyle = NO;
playercontroller.view.frame = CGRectMake(18, 20, 732.5, 360);
[self.view addSubview:playercontroller.view];
[playercontroller.moviePlayer play];
}
-(IBAction)play_3 {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"origami_panel02" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playercontroller.moviePlayer.controlStyle = NO;
playercontroller.view.frame = CGRectMake(18, 395, 233, 233);
[self.view addSubview:playercontroller.view];
[playercontroller.moviePlayer play];
}
It continues like this for a total of five videos.
Please can someone tell me how to make the video pause when another video is played. Thanks!
You can not have more than one playback active when using MPMoviePlayerController. Whereas being paused also counts as active.
You got at least these two options:
Use AVPlayer as that baby supports multiple active video playbacks concurrently.
Stick with MPMoviePlayerController but fake that pause-state by taking a snapshot image from the movie (thumbnailImageAtTime:timeOption:). Then use that image and display it instead of the player.

UIDeviceOrientation for MPMoviePlayerViewController

I am working on a app in which I have a list of items where i want to click on a thumbnail and video must be played, My requirement is Only MPMovieViewController must rotate remaining screens must not rotate for this I am using below method in list to make it portrait every time. But if i am using it i am not able to rotate the MPMoviePlayerViewController.
- (NSUInteger)supportedInterfaceOrientations{
return (UIInterfaceOrientationMaskPortrait);
}
So i returned "UIInterfaceOrientationMaskAll" after returning i am able to Autorotate the MPMoviePlayerViewController But when i come back clicking on done Button or after video is done when video is in landscape mode then my previous is rotating but i dont want that behaviour i need the previous view in portrait, Below is code for implementing the MPMoviePlayerViewContoller.
NSString *filepath = [[NSBundle mainBundle] pathForResource:[exeDict objectForKey:#"longVideoName"] ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController.moviePlayer];
[moviePlayerController.view setFrame:CGRectMake(0, 0, 320, 480)];
moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayerController.view.userInteractionEnabled =YES;
[moviePlayerController shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationMaskAll];
// AppDelegate* myDelegate = (((AppDelegate*) [UIApplication sharedApplication].delegate));
// [myDelegate.window addSubview:moviePlayerController.view];
MainViewController* mainViewController = (MainViewController*)self.mainViewDelegate;
[mainViewController presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer play];
Please let me know how to get it.
you will have have to put code in viewwillappear() method so that your list appears in portrait mode
- (BOOL)shouldAutorotate
{
return NO;
}
Implemented above code in mainViewController??

Resources