iOS pop quiz:
~90% of the time, this code will show a video.
~10% of the time, this code will show a screen with multimedia controls but no video.
If the user rotates their device, the video will then show correctly, even if the user rotates it back.
Why? And how do we fix it?
No idea yet. Let’s check the loadState, just for fun.
When it works, the loadState is 3.
When it fails, the loadState is 0.
Interesting.
For reference, this is what 3 and 0 map to:
MPMovieLoadStateUnknown
Cryptic. Let’s check the docs and see what it means.

MPMovieLoadStateUnknown
The load state is not known at this time.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
Okay. Since the documentation is “concise” at best, let’s just assume that the video isn’t ready yet, for whatever reason. And, once we’ve forced it out into the limelight, it just stands there trembling with stage fright no matter how long we wait—unless we rotate the device, which is the iOS equivalent of yelling “Snap out of it!” and delivering a firm slap to the face.
So what’s the solution? Simply check yourself, lest you wreck yourself.
It ain’t pretty, but it fixes this subtle bug that probably exists in most apps that play movies.


3 Comments
fourthversion,
That is a far more elegant solution. I don’t know why I didn’t think of that. Great tip!
Have you considered using KVO to determine when the laodState becomes ‘valid’? Just a thought. Good stuff.
This doesn’t work on iOS 5.0.1!