Multiple AVPlayers with AVPlayerLayers disappears second time push viewcontroller - iphone

I have a viewcontroller with four (4) AVPlayers (with AVPlayerLayers like APPLE example).
If I pop this viewcontroller and the push a new instance of the same type. I'm not able to play video in one or two AVPlayers. No errors and code runs fine, AVPlayerLayers also says it has a superLayer.
And to the most strange thing if I push home button, coming back to springboard and the enter the app all video players like magic start playing. It's like it rerender the view tree or something.
Any hints or clues?
PS. I wait for assets to be ready using loadValuesAsynchronouslyForKeys.

We had a similar problem. Following answer lead to the solution:
AVplayer not showing in ScrollView after 2-3 times
You have to call: [AVPlayer replaceCurrentItemWithPlayerItem:nil]; when your viewcontroller gets unloaded. This might be tricky as you might have added an observer or used addBoundaryTimeObserverForTimes:queue:usingBlock:
Also you have to be careful when checking agaings superlayer: Better check against uiview.window when determing whether your view is still attached to the view hierarchy.
yours
phil

Related

Camera turns black upon resume

My app uses multiple features for Apple's demo project AVCam. Everything works just fine except when I exit the app and go back in it, it doesn't show the camera preview anymore. Does anyone know what piece of code I am supposed to use and where it belongs? I tried searching but a lot of questions relating to android popped up.
You need to reinitialize your camera once the App becomes active again. In your app delegate methods, override, applicationDidBecomeActive and send a notification so your view controller knows that your app became active again.
Pending the notification received, you can reload the viewDidLoad, or move the contents of viewDidLoad to viewDidAppear. There's multiple ways to do this. You can also reload the contents of viewDidLoad in viewWillAppear. There's many many many ways to do this, like I said.

Where to put cleanup code in UIViewController?

I have a UIViewController subclass that loads a bunch of images for each cell in a tableview asynchronously which is handled by a separate download class. I keep a list of all of these download requests in a dictionary which is keyed to the index of the cell that is requesting the image.
My question is i where should i put the code that cancels the image download if the viewcontroller is popped off the navcontroller? I need to do this because if the user hits back while there are still images being downloaded (which could take a while) then when they are finished downloading the viewcontroller has already been released.
I cant put it in the viewWillDisappear method because i do not want to stop the download if the user clicks on a separate tab and only when the hit the back button. For now i put this code in the viewcontrollers dealloc method which works fine although it doesnt seem right for some reason. I thought of using the viewDidUnload method but it seems this is only called when there is a low memory warning?
Any ideas?
dealloc is the perfect place for this as the view controller gets deallocated and you are responsible to clean your stuff up.
You may want to consider putting the code in viewWillDisappear: anyway, after all if the screen you are navigating to needs anything loaded it will be slowed by the background image load...

MPMoviePlayerController inside UITabBarController

Ok, so here's the thing. I have a UIViewController that contains a UITabBarController. That tab bar has a UIViewController for each tab button. Now, inside one of the tab buttons is an MPMoviePlayerController that is playing a stream from over the network. Playing the stream works just fine and you can see the video and hear the audio.
The problem is when you navigate to another tab. The audio still plays, which is good, but when you go back to the stream, the video is black. The audio is still playing, but the video needs to be playing too.
Has anyone run into this problem before?
I'm currently using iOS 4.0 to build against and an iPhone 3GS.
If more information is needed, just ask and I'll do my best to answer.
Thanks,
Robbie
Strange things might happen if the view isn't on-screen (I believe it's removed from the view hierarchy when you switch tabs).
Have you tried using MPMoviePlayer's view directly? (not MPMoviePlayerController)
Can you add and remove the view and keep the movie playing?
Does pausing and resuming help?
Could you pass the view around between view controllers, or maybe make it a direct subview of the window in viewWillDisappear and move it back in viewDidAppear? (I'm not sure if the view's been removed from the hierarchy in viewWillAppear/viewDidDisappear.)
The solution I'm going ahead with is entering fullscreen mode and exiting fullscreen mode very quickly. It may not be the best thing, but it's working.
This still happens on iOS 6. If I have two movie player (one on each tab), even if they are not playing, if I switch form the first to the seconds tab and return to the first, the movie player will be black.
My solution was as simple as calling prepareToPlay on -viewDidAppear::
- (void)viewDidAppear:(BOOL)animated;
{
[super viewDidAppear:animated];
[self.moviePlayer prepareToPlay];
}
I believe this will add the shared internal view of MPMoviePlayerController to the view hierarchy.

How can I add a startup sound when UIViewController *controller is initiated?

ViewDidLoad doesnt work.
ViewDidappear Doesnt work.
No matter what happens, the sound is played then the viewController image is displayed.
What I want is the image to be displayed, then the sound plays simultaneously'
did you try viewWillAppear?
That could be because things happen in SEQUENCE unless you give explicit instructions that you want things to happen at the same time. If you want a sound to play WHILE an image is loading you need to use threads.
This guide explains how to accomplish that
I think viewWillAppear might work. viewDidLoad only gets called once, so it wouldn't get played every time a view controller is pushed.
I ended up making the sound effect have an extra 1 second pause at the beginning of it and playing it in viewdidappear. Not the perfect solution, but it was the easiest.
The sound was originally being played, before the phone actually rendered the viewcontroller, which I found counter intuitive seeing as the method 'viewdidappear' kinda sounds like the view should have erm already appeared.
Does that make sense?

UIViewController not responding to touches

Hey all, I'm completely stumped with this iPhone problem.
This is my first time building a view programmatically, without a nib. I can get the view displaying things just fine, but the darn ViewController isn't responding to touches the way it used to in programs where I used a nib. I should add that in the past, I started with the View-Based Application template, and this time I used the Window-Based Application template.
My thinking is that the View-Based template does something magical to let the iPhone know where to send the touch events, but I can't figure out what that would be even after several hours of bumbling around Google. Or I could be looking in an entirely wrong place and my troubles are related to something else entirely. Any thoughts?
There's nothing magical in the view-based template. The most likely reasons for failure to respond to touches are:
You've messed with touchesBegan:withEvent:, userInteractionEnabled, exclusiveTouch or something else, thinking you need to mess with these (generally you don't; the defaults are generally correct)
You created a second UIWindow
You put something over the view (even if it's transparent)
Simplify your code down to just creating a view programatically that responds to a touch and nothing else. It should be just a few lines of code. If you can't get that working, post the code and we'll look at what's going on.
Problem solved. touchesEnded != touchedEnded.
That'll teach me to program without my glasses on.
Another possible scenario for failure in response to touches is when your VC frame is not predefined and its boundaries are actually exceeding the window placeholder. It happens a lot when you just forget to define the frame property for the VC.
Once you define it correctly - User interaction returns to normal.
Good luck !