Can also be found on my not very often updated blog here
OK, first thing you need to know is that Cocos2d doesn’t play videos, it leaves this up to the iOS SDK on the devices. The following code therefore is Obj C which can be inserted into your projects AppController.mm file or you could create a new mm interface file and put it in there.
* Add the “AVFoundation.framework” to your projects Frameworks (located the Frameworks folder, right click, select “Add ~~> Existing Frameworks”).
* Add the “MediaPlayer.framework” to your projects Frameworks.
* You will need a video to play and to add it to the resources in the project. Use your own or download my sample video from here (MiniVideo640x360.m4v)
* Add the following code to your AppController.mm
<pre>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MPMoviePlayerViewController **playerViewController=NULL;
int g_iPlayVideoState=0;
name:MPMoviePlayerPlaybackDidFinishNotification
object:];
playerViewController.view.frame = rScreen;
cocos2d::CCDirector::sharedDirector~~>purgeCachedData;
// Add the view
EAGLView view = ;
;
;
NSLog;
// Add the view - Use these three lines for Cocos 2D X
window = initWithFrame: bounds]];
;
;
//—play movie—
MPMoviePlayerControllerplayer = ;
player.scalingMode=MPMovieScalingModeAspectFit;
player.shouldAutoplay=TRUE;
;
}
g_iPlayVideoState = 1;
NSLog;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~~ movieFinishedCallback: aNotification
{
NSLog;
MPMoviePlayerController**player = ;
removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
;
;
g_iPlayVideoState = 0;
NSLog;
}
</pre>
Call the PlayVideo function from with a line like the following. Call it from your AppController’s didFinishLaunchingWithOptions function just before the app.run;
<pre>
;
</pre>
In Appelegate.cpp, find your applicationDidFinishLaunching function and after “pDirector~~>setOpenGLView(m_pMainWnd);” add the line:
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
The above gl command sets your Cocos2D-X window to transparent wherever it is black, so you can see through it to the video behind.
I have a global variable “g_iPlayVideoState” which you can monitor in your Cocos2D applications code to detect when the video ends, its values are 2=initializing, 1=playing, 0=finished. At the end of play you might want to put the views back to non-transparent with the call “glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Hello
I try to get the Video to play in the HelloWorld project, but I only hear the Audio playback and I do not see anything on my emulator/device. Do you have any idea?(I am newbie in Object-C) .
Sounds like you are doing the right thing. I wrote this a while ago for an old versions of Cocos2DX & Xcode, will need to look at it to see what’s wrong.
Is the screen just black? Will need to look at it again, maybe in a couple of days.
As I suspected before, seems the newer iOS keeps (or newer Cocos2D-X loses) the touch dispatch events even after the movie is finished. A fix was to set the movie window layer to hidden at the end of the movie. movieFinishedCallback now looks like below:-
Hi and thanks for sharing this !
I’m new to cocos2d but so far it seams to be a really great community
I’ve tried to run the HelloWorld_MoviePlayer project Gav T has uploaded on win32 but I’m having 2 linking errors
1>AppDelegate.obj : error LNK2001: unresolved external symbol impglClearColor@16
1>HelloWorldScene.obj : error LNK2001: unresolved external symbol “int g_iPlayVideoState” (?g_iPlayVideoState@@3HA)
Maybe this is due to the fact that I did not Add the “AVFoundation.framework” and “MediaPlayer.framework” or the “equivalent” to the project but I don’t know how to do that on win32 with visual studio 2008 ( or what to add ).
If I understood correctly AVFoundation.framework and MediaPlayer.framework are from iOS SDK, so how about Android and win32 ?
Ideas of how to make it work on win32 and Android ?
Congratulations on choosing Cocos2d-x! It’s quite powerful and has helped me progress with my game, but one thing it doesn’t do is play videos. Hence, I wrote the above to play videos but it is only for iOS devices (iPhone, iPad, etc). You’ll notice the code is in a “.mm” file which is the ObjC file (iOS SDK).
So if you want to play videos on Win32 you will need to write the code for that platform yourself, there is plenty of source code and tutorials on the CodeProject here: http://www.codeproject.com/kb/audio-video/
I have not done it ( yet ) because I’ve concentrated on different parts of the projects.
However I’m still interested to see an example if someone has done it in the meanwhile.
But i got a problem when try to use it. This code setup a video play as soon as application starts right? how do i change this to play a video after sometime in the application?