Scaling a movie on the iPhone
Recently, I’ve needed to figure out a way how to play a movie on the iPhone in a non-fullscreen mode. So I have a video file and I need to play it on just a part of the screen, just as if it was any other view. I’ve searched the Internet but couldn’t find anything that would do all I needed. Most of the answers to this question simply stated it’s not possible with the current SDK. I’ve found a method using private APIs which is not an option because we need to have this approved on the AppStore. Another idea suggested rolling our own software decoder to play the video. That was also not an option because it would be too CPU-intensive, require too much work and be too hard on the battery.
I was about to give up when I found the answer in a place where I expected it the least – an official Apple example.
Well, what the example really does is it shows how to add custom controls on top of a movie. The way it’s done seems more like a workaround than the way it was designed to work, but it’s better than nothing
The idea is to create the movie player, then get a list of windows in the application and the “key” window is our movie player. Here is the code from the Apple example:
-
NSArray *windows = [[UIApplication sharedApplication] windows];
-
UIWindow *moviePlayerWindow = nil;
-
if ([windows count] > 1)
-
{
-
moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
-
}
From here, you can easily add your own subviews to the moviePlayerWindow. However, that’s not exactly what I’ve promised.
Now resizing the window didn’t quite work as expected. Trying to set the bounds or frame only changes the center of the window, but doesn’t really affect the size of the movie.
There is one property though that works pretty well – the window’s transform. So here is the code:
-
[moviePlayerWindow setTransform:CGAffineTransformMakeScale(0.5, 0.5)];
You could also rotate the video the same way. Also, you can still add your custom controls to this window and it works great (note though that they will be transformed too). There is only one problem: it seems the movie player somehow messes up the status bar so it becomes completely white – I haven’t yet been able to fix that but hopefully you can work around it by placing your movie above it or simply hiding it.
I hope this has saved you some time and let me know if you have any comments/suggestions.




[...] This post was mentioned on Twitter by Ivan Galic, MEE. MEE said: RT @nightirion: New developer tip at NightIrion.com: How to play a movie in a non-fullscreen mode: http://bit.ly/6ntKdY [...]
Pingback by Tweets that mention NightIrion ยป Scaling a movie on the iPhone -- Topsy.com — January 5, 2010