UIDeviceOrientation for MPMoviePlayerViewController - ios

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??

Related

Set orientation of MPMoviePlayerController

My whole application is in portrait mode only and i am playing youtube video in my application. For you tube I am using UIWebview. When user click on play button in UIWebview it automatically launch the MPMoviePlayerController. So I did not declared any MPMoviePlayerController object. So I want MPMoviePlayerController support both portrait and landscape orientation. So please suggest.
If you use NavigationController, you could subclass it and do the following:
#import "MainNavigationController.h"
#import <MediaPlayer/MediaPlayer.h>
#implementation MainNavigationController
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
#end
Then you should set your app to support all orientations, and this code will allow orientation change only if it is playing your movie 'MPMoviePlayerController`.
When calling your movie you should send a notification, so if user closes it in any orientation other than portraitit switches back to portrait.
Something like this:
- (IBAction)playButton:(id)sender {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.player.moviePlayer];
NSURL *url = [NSURL URLWithString:videoUrl];
self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:self.player];
}
-(void)moviePlaybackDidFinish
{
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
forKey:#"orientation"];
}
This should do it for you, let me know how it goes.

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.

Play background video in iphone App

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..

MPMoviePlayerController - Split view controller should have its children set before layout

What is the appropriate point to send the play message to a MPMoviePlayerController instance instantiated in a splitView detail View Controller?
My app is receiving the above console message (with a !) but not crashing...
The app is utilizing MPMoviePlayerController to play a movie from an asset URL
and the responsilble method is called as follows:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self startPlayingVideo:self];
}
It plays the video just fine but the console message is looming...
If I move the method call to viewWillAppear:animate:, the console message does not show up. The issue is now I can only hear audio and do not see the video.
For completeness, here is the called method...
- (void) startPlayingVideo:(id)sender
NSURL *movieURL = [NSURL URLWithString:self.movieURLString];
if (self.moviePlayer != nil){
[self stopPlayingVideo:nil];
}
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL ];
if (self.moviePlayer != nil){
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(videoHasFinishedPlaying:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
} else {
NSLog(#"Failed to instantiate the movie player.");
}
}
My original issue stemmed from having the MoviePlayerController as an entirely different viewController (embedded in a detail navigation controller). I redesigned the parent view to include a moviePlayer child view. This solved the issue.

sound only playing through headphones on certain devices with MPMoviePlayerController

I have a weird problem for you all.
MPMoviePlayerController is playing the video fine, and the audio ONLY plays through headphones.
The real drag is that this only happens on some iPads and iPhones, even the SAME EXACT MODELS
running the SAME EXACT SYSTEM!
I've created a simple failing example here:
http://www.porcaro.org/MPMoviePlayerController/TestMovie.zip
I've seen it run fine and fail on iPhone 4S, iPhone 4 and iPad 2.
Here is the most relevant code. Thanks for any insight, I'm going to submit a bug to Apple as well:
(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
moviePath = [NSString stringWithFormat:#"%#/intro.m4v", [[NSBundle mainBundle] bundlePath]];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
theMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
controlStyle = MPMovieControlStyleEmbedded;
movieView = [self view];
movieRect = [[self view] frame];
controlStyle = MPMovieControlStyleFullscreen;
theMoviePlayer.controlStyle = controlStyle;
theMoviePlayer.view.userInteractionEnabled = YES;
if (1) {
NSLog(#"Created theMoviePlayer: %#. Playing: %#", theMoviePlayer, moviePath);
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(checkForEndOfMovie:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:theMoviePlayer];
// this line doesn't fix the problem
//[theMoviePlayer prepareToPlay];
[[theMoviePlayer view] setFrame:movieRect];
[movieView addSubview: [theMoviePlayer view]];
[theMoviePlayer play];
}
This is an old question but maybe it'll help someone. I came across the same issue and found out it was happening when the phone is in silent mode only.
Solution is to set the player's useApplicationAudioSession property to false.
[theMoviePlayer setUseApplicationAudioSession:NO];
This problem was happen to me once playing video on iPad 2 using mpMoviePlayer. The video was playing perfectly but audio only comes when earphones connected.
Solution to solve the problem:
[theMoviePlayer setUseApplicationAudioSession:NO];

Resources