I have a UITableView with custom cells,in this table a list of video is showing 10 by 10 from server using pagination concept. when you click on video list then video start playing in moviePlayerController but when you click on done Button of moviePlayerController then your video list is refreshed .
I want to stop this refreshing and want to see UITableView same as before playing video.
Please see the code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
CurrentVideoIndex =indexPath;
NSDictionary *VideoUrlDic = [videoList objectAtIndex:indexPath.row];
NSString *VideoUrl = [VideoUrlDic objectForKey:#"playUrl"];
//NSLog(#"VideoUrl is ::%#",VideoUrl);
NSURL *fileURL = [NSURL URLWithString:VideoUrl];
moviePlayerController= [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[[moviePlayerController moviePlayer] prepareToPlay];
//[[moviePlayerController moviePlayer] setUseApplicationAudioSession:NO];
[[moviePlayerController moviePlayer] setShouldAutoplay:YES];
//[[moviePlayerController moviePlayer] setControlStyle:2];
//[[moviePlayerController moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
}
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[moviePlayerController.moviePlayer stop];
moviePlayerController = nil;
[[NSNotificationCenter defaultCenter] removeObserver:moviePlayerController name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController.moviePlayer];
if (![[self modalViewController] isBeingDismissed])
{
[self dismissMoviePlayerViewControllerAnimated];
[moviePlayerController.view removeFromSuperview];
[self hideProgress];
}
// [self.tableView scrollToRowAtIndexPath:CurrentVideoIndex atScrollPosition:UITableViewScrollPositionTop animated:NO];
//loading=NO;
}
#pragma UIScroll View Method::
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (!loading) {
float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
if (endScrolling >= scrollView.contentSize.height)
{
[self performSelector:#selector(loadDataDelayed) withObject:nil afterDelay:2];
}
}
}
#pragma UserDefined Method:::
-(void)loadDataDelayed{
counter +=10;
NSString *count = [NSString stringWithFormat:#"%d",counter];
NSLog(#"count value after Scrolling is :::%#",count);
[self createConnection:count];
}
When the video player is opened the presentmodalviewcontroller is opened and when you press the done button it dismiss the modalviewcontroller due to which whatever you have initialize would be released and again the whole view is called, so to avoid that write the code in viewwillappear method.
Which would create the tableview when you press the done button once again, thats the only solution when you dismiss any controller. In viewwillappear method use
NSLog(#"");
statement you would come to know what i am trying to say
Related
My problem is the following: managing properly the app states.
I have several xibs implementing MPMoviePlayerViewcontroller. The light (that weight around 100kb) looping videos autoplay when the app comes back to the foreground (I've already handled the app states so that works fine).
In the 1st Xib the video plays immediately when coming back to the foreground. The 2nd Xib takes more time. The 3rd Xib takes even more time to continue autoplay and in the 4th Xib onwards it takes like 10 seconds to autoplay. When the app comes back from the the background to the foreground it takes a lot of time with a black screen until it starts auto play.As if one Xib affects the other.
I do the same code in every xib and as I advance it takes longer for the player continue auto play. Note that I overlay buttons above the player to go back or next. How to solve the lag explained previously?
AppDelegate.h
- (void)applicationWillResignActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"WillResignActive" object:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"WillResignActive" object:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"WillEnterForeGround" object:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"DidBecomeActive" object:nil];
}
ViewController1.h
#synthesize playerController;
-(IBAction)next
{
two *back = [[two alloc]initWithNibName:#"two" bundle:Nil];
back.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:back animated:YES completion:nil ];
[back release];
}
- (void)viewDidLoad{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"mov"]];
playerController = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
[self.view insertSubview:playerController.view atIndex:0];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerController.moviePlayer.scalingMode = MPMovieScalingModeFill;
playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
playerController.moviePlayer.view.userInteractionEnabled = NO;
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(AppDidBecomeActive) name:#"DidBecomeActive" object:nil];;
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(EnteredBackground) name:#"WillResignActive" object:nil];;
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(EnteredForeground) name:#"WillEnterForeGround" object:nil];;
[playerController.moviePlayer prepareToPlay];
[playerController.moviePlayer prepareToPlay];
[playerController.moviePlayer play];
}
-(void)AppDidBecomeActive{
if(playerController.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted || playerController.moviePlayer.playbackState == MPMoviePlaybackStateStopped || playerController.moviePlayer.playbackState == MPMoviePlaybackStatePaused)
{
[playerController.moviePlayer play];
}
}
-(void)EnteredBackground
{ [playerController.moviePlayer pause];
}
-(void)EnteredForeground
{ [playerController.moviePlayer play];
}
I have been able to successfully play a video on start of the app by calling a function that plays the video in the viewDidLoad method. But on doing so, i am unable to see the "Done" button. The "Done" button only seems to appear if i call that same function with the help of a button. I am posting the code below. How will I make this done appear by calling the same function in the viewDidLoad method? What have i been doing wrong? Thank you!
in my viewcontroller.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMedia];
}
- (void) playMedia {
//hide status bar first
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//resets again in playMediaFinished
movieFile = [[NSBundle mainBundle] pathForResource:#"sec" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath: movieFile];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setFullscreen:NO animated:YES];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
CGAffineTransform transform = self.view.transform;
[moviePlayer.view setFrame:CGRectMake(0, 0, 480, 320)];
moviePlayer.shouldAutoplay = YES;
NSLog(#"Should be playing Movie");
}
- (void) playMediaFinished: (NSNotification*) theNotification {
moviePlayer = [theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer
respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
[playerView removeFromSuperview];
//reset status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
NSLog(#"Should be DONE playing Movie");
}
Try doing this using viewDidAppear instead of viewDidLoad. I'm not surprised starting a video in viewDidLoad is causing unexpected behavior considering you're presenting the video before the main view appears on screen. Keep in mind, the view is done loading before it is actually visible.
I am creating a MPMoviePlayerController object and streaming a video in full screen mode.
I am using a UIViewController to display the movie view.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//http://www.youtube.com/watch?feature=player_detailpage&v=ebeQaznNcmE
NSURL *url = [NSURL URLWithString:#"http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4"];
MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
mPlayer.view.frame = gMainView.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mPlayer];
mPlayer.shouldAutoplay = YES;
mPlayer.controlStyle = MPMovieControlStyleFullscreen;
[gMainView addSubview:mPlayer.view];
[mPlayer prepareToPlay];
[mPlayer setFullscreen:YES animated:YES];
[mPlayer play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
//movie finished playing
}
else if (reason == MPMovieFinishReasonUserExited) {
//user hit the done button
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
}
else if (reason == MPMovieFinishReasonPlaybackError) {
//error
}
}
When clicking done, the video visual is removed from the screen, but the controls are not removed from the screen and the view is not removed from the screen.
The control does come to "//user hit the done button". It does execute the code to remove the view from superview, I checked by adding logs, but the controls are not removed from the screen and the view is not removed from the screen.
What am I doing wrong?
EDIT:
If I use MPMoviePlayerViewController then it doesn't even wait for me to press Done. Once the video is complete it automatically removes the view. But I don' want that.
EDIT:
If I remove "[mPlayer setFullscreen:YES animated:YES]" then when clicking on Done, the view is removed completely. But the video is not displayed in full screen and the status bar goes gray which is again what I don't want.
The below code worked for me, Hope it helps you too.
-(IBAction)playVedio:(id)sender{
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentMoviePlayerViewControllerAnimated:mp];
}
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[mp.moviePlayer stop];
mp = nil;
[mp release];
[self dismissMoviePlayerViewControllerAnimated];
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
id presentedViewController = [window.rootViewController presentedViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if (window && [className isEqualToString:#"AVFullScreenViewController"]) {
return UIInterfaceOrientationMaskAll;
} else {
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
}
else if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
}
return UIInterfaceOrientationMaskPortrait;
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize size = frame.size;
NSLog(#"%#", [NSString stringWithFormat:#"Rotation: %s [w=%f, h=%f]",
UIInterfaceOrientationIsPortrait(interfaceOrientation) ? "Portrait" : "Landscape",
size.width, size.height]);
}
}
My Video works when I click my play button, and I know the code for my view change works. I just can't seem to get the view to change after my video is done playing, not sure what is going wrong. Any ideas to get the view to change once the video is done playing?
My code to play movie
-(IBAction)playMovie:(id)sender {
NSString *movieUrl = [[NSBundle mainBundle] pathForResource:#"Movie_1666" ofType:#"m4v"];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:movieUrl]];
//Smoothe Transition
//[self presentMoviePlayerViewControllerAnimated:playerController];
//Instant Transistion
[self presentModalViewController:playerController animated:NO];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playerController.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerController]; }
My method to change views
- (void)playbackFinished:(NSNotification*) notification {
MPMoviePlayerController *playerController = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerController];
View2 *view2 = [[View2 alloc] initWithNibName:#"View2" bundle:nil];
View2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:view2 animated:YES]; }
EDIT:
My Notification object was wrong and wasn't triggering my playbackFinished method
this change fixes that.
- (void)playbackFinished:(NSNotification*) notification {
playerController = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerController];
I also put this in my header file to make it global for use in my playbackFinished method
MPMoviePlayerViewController *playerController;
Once the movie play finished, dismiss the playerController view without animation. Also check, whether you are presenting the View2 in main thread.
Edit:
Dismiss the playerController by
[playerController dismissModalViewControllerAnimated:NO];
I make mpmovieplayerviewcontroller and load movie in it (from stream). Now I have call to setContentURL method that change URL and it works. Whenn app is first time started it shows view with button after button is pressed it opens movie player and load movie. But when I call code where is setContentURL player close it self first, go back to first view where I have to again click play button to again open player (so it remembers and load new URL). How to prevent player to not close it self when switching URL? This must be some small issue, please help :)
This is code when play button is clicked (first view)
-(void)initializeMovieFromStream:(NSString *)var
{
if(player != nil)
{
NSString *title = [[NSString alloc] initWithFormat:#"%#%#%#", #"http://"];
NSURL *nurl = [NSURL URLWithString:title];
NSLog(#"Switching channel...");
[player.moviePlayer setContentURL:nurl];
isMoviePaused = NO;
}
else
{
NSString *title = [[NSString alloc] initWithFormat:#"%#%#%#", #"http://"];
NSURL *nurl = [NSURL URLWithString:title];
player = [[MPMoviePlayerViewController alloc] initWithContentURL:nurl];
[player.moviePlayer.view.window setUserInteractionEnabled:YES];
player.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
}
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(movieFinishedPlaying:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[player moviePlayer]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(nowPlayingMovieDidChange:)
name:MPMoviePlayerNowPlayingMovieDidChangeNotification
object:nil];
[self presentModalViewController:player animated:YES];
//[self presentMoviePlayerViewControllerAnimated:player];
UIView *mv = player.view;
//[mv setFrame:CGRectMake(0, 0, 320, 480)];
[mv addSubview:myOverlayChannelPicker];
[mv bringSubviewToFront:myOverlayChannelPicker];
// register this class to observe TestNotification that comes from OverlayChannelPicker
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveTestNotification:)
name:#"TestNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(pauseStreamNotificationHandler:)
name:#"notiPauseStream"
object:nil];
}
-(void) movieFinishedPlaying: (NSNotification *)note
{
[[NSNotificationCenter defaultCenter]
removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:[player moviePlayer]];
//[player release];
}
When URL need to be switch I use this nitification handler
- (void) receiveTestNotification:(NSNotification *) notification
{
NSString *stringFromNote = (NSString *)[notification object];
[switchableChannel setString:stringFromNote];
[self initializeMovieFromStream:stringFromNote];
}
It is solved I just change animation property to NO.