iPhone Movie Playback - Here it is.
by Sven Bergström · in iTorque 2D · 10/23/2009 (9:18 pm) · 48 replies
One thing that was always a cool idea was playing cinematics and, intro movies in an iPhone game. Up until now there have been people planning to add it but i had already started so here it is.
Notes
The code is asynchronous - Meaning that the game/other stuff still runs in the background. This is great for loading stuff in the background while the movie is playing :) One thing i will add is that a movieID will be returned, and call the console function for movieDidFinishPlayback(%ID), so that you can time things better.
Todo
make it use ~/ paths.
maybe add datablocks for videos
Step 1 : , you need to add the two files to your project tree.


Step 2 : , you need to add the MediaPlayer Framework.
On the target -> Right click -> Get Info
Select the general tab
Make sure you are adding from the iPhoneOS SDK

Step 3 : , Build and go.
NO EXTENSION is passed in, m4v is the only one atm, it looks in the bundle based on the file name, starting at / (which is "Resources" in XCode.
Someone asked about it being modal and not dismissable, if you set the controlsMode to hidden or volume only there is no way to dismiss the movie (meaning you can force them to watch it). The other thing i will add is the callback for handling when they are attempting to dismiss the video from modal mode as well - So that you can perhaps show another loading screen if your background loading is not complete.
There is only one function needed. The scalemode and controlMode are from the OS :
For scaling - None, Aspect Fit, Aspect Fill, Fill
For controls - Default controls, Volume Only, Hidden
Code side :
Script Side
Quite simple , just use iTunes.
Step 1 : ,
Add a video file by opening it in iTunes

Step 2 : ,
Change the view to be this view, so you can see the "Movies" tab on the left, and the items in a list format.

Step 3 : ,
Right click the video and convert it to iPhone/iPod version


Always check the settings!

Step 4 : ,
Rinse and repeat :)

Download
Notes
The code is asynchronous - Meaning that the game/other stuff still runs in the background. This is great for loading stuff in the background while the movie is playing :) One thing i will add is that a movieID will be returned, and call the console function for movieDidFinishPlayback(%ID), so that you can time things better.
Todo
make it use ~/ paths.
maybe add datablocks for videos
"Installation"
Step 1 : , you need to add the two files to your project tree.


Step 2 : , you need to add the MediaPlayer Framework.
On the target -> Right click -> Get Info
Select the general tab
Make sure you are adding from the iPhoneOS SDK

Step 3 : , Build and go.
Usage
NO EXTENSION is passed in, m4v is the only one atm, it looks in the bundle based on the file name, starting at / (which is "Resources" in XCode.
Someone asked about it being modal and not dismissable, if you set the controlsMode to hidden or volume only there is no way to dismiss the movie (meaning you can force them to watch it). The other thing i will add is the callback for handling when they are attempting to dismiss the video from modal mode as well - So that you can perhaps show another loading screen if your background loading is not complete.
There is only one function needed. The scalemode and controlMode are from the OS :
For scaling - None, Aspect Fit, Aspect Fill, Fill
For controls - Default controls, Volume Only, Hidden
Code side :
//If you going to code it, just look at the parameters in the code :) #include "platformiPhone/iPhoneMoviePlayback.h" playMovie(...);
Script Side
//You will have to use numbers or constants here for now playiPhoneMovie( %filename, %scaleMode, %controlMode );
Creating a movie for the game
Quite simple , just use iTunes.
Step 1 : ,
Add a video file by opening it in iTunes

Step 2 : ,
Change the view to be this view, so you can see the "Movies" tab on the left, and the items in a list format.

Step 3 : ,
Right click the video and convert it to iPhone/iPod version


Always check the settings!

Step 4 : ,
Rinse and repeat :)

Downloads
Download
#2
10/24/2009 (12:06 am)
Nice! We're getting this into the next proper release, right?
#3
10/24/2009 (3:52 am)
Awesome !!!!!!
#4
10/24/2009 (3:28 pm)
Fantastic! Very clean integration.
#5
@Sven.
I was hoping to find the time to write a little tutorial during the week about how to use the shake detecting functionality of the SDK in Torque. Have you got this done already? Just checking so we aren't duplicating work :)
It is already working in my codebase in its simplest form. Just not hooked up into the Torque event system as I really didn't need (or desire) it to be for what I use it for.
10/25/2009 (6:15 pm)
Cool stuff! Might give this a shot sometime - does it load up vids nice and quickly? (stream) I haven't read up on playing videos on the iPhone yet.@Sven.
I was hoping to find the time to write a little tutorial during the week about how to use the shake detecting functionality of the SDK in Torque. Have you got this done already? Just checking so we aren't duplicating work :)
It is already working in my codebase in its simplest form. Just not hooked up into the Torque event system as I really didn't need (or desire) it to be for what I use it for.
#6
Shake detection would be welcome - nice way to fire off special attacks in games :)
10/25/2009 (7:23 pm)
@Craig: Yes, this streams. It's the only way the built-in functions play movies.Shake detection would be welcome - nice way to fire off special attacks in games :)
#7
Yes, personally I use it to pause my game and display menu options.
The shake in the SDK is actually pretty dumb and doesn't allow you to tweak its tolerance value or even get data back from it about a direction the shake occurred back and forth in etc. I suppose this does give the advantage of being the same experience (Apple are way up on that sort of thinking) across any app that decides to utilise it.
Ultimately I'll be using a "simplistic gestures" library I'm writing as part of my day job which supports nudges (little directional movements) as well as tracking and classifying lines traced on a screen etc, but I digress :)
10/25/2009 (9:11 pm)
@RonnyYes, personally I use it to pause my game and display menu options.
The shake in the SDK is actually pretty dumb and doesn't allow you to tweak its tolerance value or even get data back from it about a direction the shake occurred back and forth in etc. I suppose this does give the advantage of being the same experience (Apple are way up on that sort of thinking) across any app that decides to utilise it.
Ultimately I'll be using a "simplistic gestures" library I'm writing as part of my day job which supports nudges (little directional movements) as well as tracking and classifying lines traced on a screen etc, but I digress :)
#8
Also, you can code your own shaking control scheme based on accelerometer, it does have the average velocity along an axis which is nice for telling if there was a shake and which way (there are many samples online for this kind of gameplay too).
Back on topic - The video should be able to take a URL for streaming from the internet as well (might even be able to now, but i want to add error checking etc).
10/26/2009 (1:51 am)
@Craig, i haven't implemented any shake functionality yet personally.Also, you can code your own shaking control scheme based on accelerometer, it does have the average velocity along an axis which is nice for telling if there was a shake and which way (there are many samples online for this kind of gameplay too).
Back on topic - The video should be able to take a URL for streaming from the internet as well (might even be able to now, but i want to add error checking etc).
#9
I know this is completely off topic but since you guys were talking about it I'll make my post short:
I was able to detect a shake by placing the following code at the end of iPhoneOGLVideo.mm:
Where kAccelerationThreshold is equal to 2.2.
Alright, back on topic :)
10/26/2009 (8:24 am)
@Craig, RonnyI know this is completely off topic but since you guys were talking about it I'll make my post short:
I was able to detect a shake by placing the following code at the end of iPhoneOGLVideo.mm:
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration {
if (acceleration.x > kAccelerationThreshold
|| acceleration.y > kAccelerationThreshold
|| acceleration.z > kAccelerationThreshold) {
Con::executef(1,"shake");
}
}Where kAccelerationThreshold is equal to 2.2.
Alright, back on topic :)
#10
10/26/2009 (8:30 am)
Thanks Justin, short and sweet. Iv added this to the list of useful tips i will post in a blog sometime.
#11
I use the console function in the following way...
%movie = "game/data/images/astro_bugz";
%isPlayed = playiPhoneMovie( %movie, "None", "Default controls" );
In my console.log I get "playiPhoneMovie : cannot find the file game/data/images/astro_bugz in the bundle"
The file is in the bundle. I also tried with extension and got the same error.
Where should I place the file?
Also, in order to build I had to edit iPhoneMoviePlayback.mm .
//#include "iPhoneVideoPlayback.h"
#include "iPhoneMoviePlayback.h"
10/26/2009 (8:51 pm)
Sven, I've placed my m4v in game/data/images/astro_bugz.m4vI use the console function in the following way...
%movie = "game/data/images/astro_bugz";
%isPlayed = playiPhoneMovie( %movie, "None", "Default controls" );
In my console.log I get "playiPhoneMovie : cannot find the file game/data/images/astro_bugz in the bundle"
The file is in the bundle. I also tried with extension and got the same error.
Where should I place the file?
Also, in order to build I had to edit iPhoneMoviePlayback.mm .
//#include "iPhoneVideoPlayback.h"
#include "iPhoneMoviePlayback.h"
#12
If you look inside the bundle is there a "Content/Resources/" type setup or is it STRAIGHT in the bundle, like "bundle root/game/data/images/astro_bugz".
Ill test the code now, and see what it returns in both cases and try find the file. Thanks for testing this.
10/27/2009 (4:24 am)
OH yea, thanks Eyal. I renamed the files at the last minute to match the functions :) EDIT : Uploaded the fixed one : thanks!If you look inside the bundle is there a "Content/Resources/" type setup or is it STRAIGHT in the bundle, like "bundle root/game/data/images/astro_bugz".
Ill test the code now, and see what it returns in both cases and try find the file. Thanks for testing this.
#13
10/27/2009 (11:16 am)
In my bundle I have "bundle root/game/data/images/astro_bugz.m4v" do I need to remove the m4v extension? I removed it from the string that I'm using to call playIphoneMovie()
#14
%movie = "astro_bugz";
playiPhoneMovie( %movie, 0,0 );
This is working great!
Thanks Sven.
10/27/2009 (12:17 pm)
ok. so I placed the actual file in the root dir of my actual game. referenced it in xCode straight into /resources/astro_bugz.m4v and called it from script.%movie = "astro_bugz";
playiPhoneMovie( %movie, 0,0 );
This is working great!
Thanks Sven.
#15
10/27/2009 (12:26 pm)
Cool, i think i know why it might be doing that. I will check the locations in the debugger and see where its actually looking instead.
#16
Really though, great work! I know the community is going to seriously dig this one.
10/27/2009 (3:34 pm)
@Sven: We were about to post our solution soon, and you beat us to the punch. :)Really though, great work! I know the community is going to seriously dig this one.
#17
10/27/2009 (4:03 pm)
@Dave : I figured with you being a little hidden more recently you might enjoy it too! Any ideas/fixes/suggestions feel free to shoot them this way
#18
would it be possible to have it inside a gui? or have a gui overlay on top of it. like a big "BUY NOW" button when you are running an add for your game.
Again, this feature is not important and I can easily live without it. right now I'm just running the movie and as soon as it's over I pop my BUY NOW button.
10/27/2009 (5:15 pm)
I got some suggestion, since nobody asked me :)would it be possible to have it inside a gui? or have a gui overlay on top of it. like a big "BUY NOW" button when you are running an add for your game.
Again, this feature is not important and I can easily live without it. right now I'm just running the movie and as soon as it's over I pop my BUY NOW button.
#19
I'm back now though, and getting caught up on community matters. It looks like our implementations are pretty similar, actually... the only difference is we've been porting our videos to m4v using iMovie, and when our movie ends, we have a script callback that occurs in case we needed anything specific to happen when it returned to the game.
Awesome work again, Sven and Luma!
10/27/2009 (6:59 pm)
@Sven: 20 hour work days will make one disappear for sure. ;)I'm back now though, and getting caught up on community matters. It looks like our implementations are pretty similar, actually... the only difference is we've been porting our videos to m4v using iMovie, and when our movie ends, we have a script callback that occurs in case we needed anything specific to happen when it returned to the game.
Awesome work again, Sven and Luma!
#20
I'd be curious to hear what Sven has to say about your question, Eyal - since, well, this is his thread, LOL.
10/27/2009 (7:15 pm)
@Eyal: That would require a custom video implementation (at least I believe it would), as this is using the existing iPhone API calls to the built-in video player of the device. You MIGHT be able to work something using your own .xib and some Objective-C magic, though I haven't tried that on my end. (That said, I have been tempted to try modifying the MainWindow.xib to be able to show a video while the app is loading...).I'd be curious to hear what Sven has to say about your question, Eyal - since, well, this is his thread, LOL.
Torque Owner BeyondtheTech
Default Studio Name