Game Development Community

[Solved] Video Notifications

by Rami Bokhari · in iTorque 2D · 01/18/2012 (8:24 am) · 9 replies

hey guys, how can I notify my game that the video is finished playing?
I used playiPhoneMovie to play my video. Thanks in advance :)

About the author

I love video games.. video games are my life.. playing can't satisfy me anymore.. thank you GG helping me making my dream worlds a reality


#1
01/18/2012 (11:34 am)
In iphoneMoviePlayback.h at the end of file add :

bool getisVideoPlaying();

Then in iphoneMoviePlayback.mm code :

add row after @synthesize movieController; add :

static bool isplaymovie= false;

and add function :

ConsoleFunction(getisVideoPlaying, bool, 1, 1, "() returns true if Video playing, else false")
{
	bool retValue = isplaymovie;
	return retValue;
}
in playMovie(const char* fileName, iPhoneMovieScalingMode scalingMode, iPhoneMovieControlMode controlMode)
add

.....
isplaymovie = true;
....

then where you close video :
.......
	[super dealloc];
	isplaymovie = false;
	
}

by script in game :

function checkVideoPlaying()
{ 
  if (!getisVideoPlaying())
  {

   // Video is finished !!!
  
  }
  else
   schedule(10, 0, checkVideoPlaying);
}

look at here :

www.garagegames.com/community/resources/view/21472

^_^
#2
01/18/2012 (12:12 pm)
Thank you very much Andrea :D you are a Savior
#3
01/18/2012 (12:33 pm)
man I copied ur .h and .mm and building fails :/
#4
01/18/2012 (12:34 pm)
iTGB 1.4.1
#5
01/18/2012 (12:41 pm)
if you can post error .....

then i suggest you don't use the new mm and h in the resource but use the modifications posted here
#6
01/18/2012 (2:25 pm)
man I followed your modifications and it works like a charm now :)

here are my files:

iPhoneMoviePlayback.h
//
//  iPhoneVideoPlayback.h
//  iTorque 2D
//
//  Created by Sven Bergstrom, improved by Michael Perry
//  GarageGames, LLC. All rights reserved.
//

//These correspond to the docs over on Apples site : 
#ifdef TORQUE_ALLOW_MOVIEPLAYER

#import <MediaPlayer/MPMoviePlayerController.h>
#import <UIKit/UIDevice.h>
bool playiPhoneMovie(const char* fileName, const char* extension, MPMovieScalingMode scalingMode, MPMovieControlStyle controlStyle);
#endif//TORQUE_ALLOW_MOVIEPLAYER
bool getisVideoPlaying();
#7
01/18/2012 (2:26 pm)
iPhoneMoviePlayback.mm
//
//  iPhoneVideoPlayback.mm
//  iTorque 2D
//
//  Created by Sven Bergstrom, improved by Michael Perry
//  GarageGames, LLC. All rights reserved.
//

#ifdef TORQUE_ALLOW_MOVIEPLAYER
//iPhone media stuff
#include "iPhoneMoviePlayback.h"
#import "MediaPlayer/MPMoviePlayerController.h"
#import "MediaPlayer/MPMoviePlayerViewController.h"
#import "AudioToolbox/AudioServices.h"

//#include "audio/audio.h"

//#include "core/strings/stringFunctions.h"

//Torque Stuff
#include "console/console.h"

#include "platformIPhone.h"
extern iPhonePlatState platState;


///ObjC section below
//Declaring the interface here, so that i can use the header for inclusion in C++ code
@interface iPhoneMoviePlaybackDelegate : NSObject {
	
	MPMoviePlayerViewController* movieViewController;
	
}

@property (nonatomic, retain) MPMoviePlayerController*  movieController;

-(NSURL *) urlForLocalFile:(NSString*)fileLocation withFileType:(NSString*)fileType;
-(BOOL) playAVideo:(NSString*)fileName withFileType:(NSString*)fileType withScalingMode:(MPMovieScalingMode)scalingMode withControlStyle:(MPMovieControlStyle)controlStyle;
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;

@end


@implementation iPhoneMoviePlaybackDelegate 

@synthesize movieController;

static bool isplaymovie= false; 

    ConsoleFunction(getisVideoPlaying, bool, 1, 1, "() returns true if Video playing, else false")  
    {  
        bool retValue = isplaymovie;  
        return retValue;  
    }  

//Wanted to keep the ObjC stuff neat and not littered with C++, implemented the C++ inside the playVideo function instead (the one exposed to the engine).
-(NSURL *)urlForLocalFile:(NSString*)fileLocation withFileType:(NSString*)typeName
{
	NSURL* movieURL = nil;
	//Find the file location within our application bundle
        NSBundle *bundle = [NSBundle mainBundle];
        if (bundle)
        {
			//Actually, we need to use inDirectory or else it only looks in the toplevel directories.
			//Cut the file name from the end of the string
			NSString* fnameOnly = [fileLocation lastPathComponent];
			NSString* pathOnly = [fileLocation stringByDeletingLastPathComponent];
			
			//Ask the bundle for the path to the file
            NSString *moviePath = [bundle pathForResource:fnameOnly ofType:typeName inDirectory:pathOnly];
			
            if (moviePath)
            {
                movieURL = [NSURL fileURLWithPath:moviePath];
            }
        }
    
	//Check if its nil when you use it, if there was some kind of error
    return movieURL;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
	return YES;
}

#8
01/18/2012 (2:27 pm)
//This is the last step of the video call, the C++ hands in the values we want here (scaling etc). NO EXTENSION on the filename.
-(BOOL) playAVideo:(NSString*)fileName withFileType:fileType withScalingMode:(MPMovieScalingMode)scalingMode withControlStyle:(MPMovieControlStyle)controlStyle
{
	NSString* defaultFileType = fileType; 
	NSURL* filePath = [self urlForLocalFile:fileName withFileType:defaultFileType];
	if(filePath == nil)
	{
		return NO;
	}
	
	movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL: filePath];

	[[NSNotificationCenter defaultCenter]
			addObserver: self
			selector: @selector(moviePlaybackDidFinish:)
			name: MPMoviePlayerPlaybackDidFinishNotification 
			object: nil];
	
	//Disable audio session 
	AudioSessionSetActive(NO);
	//Audio::OpenALShutdown();
	
	[movieController setScalingMode:MPMovieScalingModeAspectFit];
	[movieController setControlStyle:MPMovieControlStyleNone];

	[platState.Window addSubview:movieViewController.view];
	
	//[movieViewController setControlStyle:MPMovieControlStyleNone];	
		
	[movieController prepareToPlay];
	[movieController play];
	//[[movieViewController moviePlayer] play];
	
	return YES;
}

//Notification of when the video has done playing now, and we can move on.
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
	//Remove our observer
	[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
	[movieViewController.view removeFromSuperview];

	// Reactivate the current audio session
	AudioSessionSetActive(YES);
	//Audio::OpenALInit();
	
	//We can dealloc this whole delegate once we are done as the playback was asynchronous, which releases the movieController
	[self dealloc];
}

- (void)dealloc
{
	//clean up
	//[movieController release];
	[movieViewController release];
	[super dealloc];
      isplaymovie = false;
}

@end



//C++ Section below
bool playMovie(const char* fileName, const char* extension, MPMovieScalingMode scalingMode, MPMovieControlStyle controlStyle)
{	
	if((fileName != NULL) && (strlen(fileName) > 0))
	{
		//For now we assume the m4v format, as that is how iTunes exports it. 
		// todo : make this try the other supported extensions, and or load without one. - Sven
		NSString* filePath = [[NSString alloc] initWithUTF8String:fileName];
		NSString* fileType = [[NSString alloc] initWithUTF8String:extension];
		
		iPhoneMoviePlaybackDelegate* playMovieDelegate = [iPhoneMoviePlaybackDelegate alloc];
		
		BOOL success = [playMovieDelegate playAVideo:filePath withFileType:fileType withScalingMode:scalingMode withControlStyle:controlStyle];
		
		if(success == NO)
		{
			Con::errorf("playiPhoneMovie : cannot find the file %s in the bundle", fileName);
		}
		
		[filePath release];
		
		//Continue with success
           isplaymovie = true;
		return true;
	}
	
	return false;
}


//Make it accessible to scripts!
ConsoleFunction(playiPhoneMovie, bool, 5, 5, "playiPhoneMovie(%filename, %filetype, %scaleMode, %controlMode) Returns True if the file was found, false otherwise")
{
	MPMovieScalingMode scale = (MPMovieScalingMode)dAtoi(argv[3]);
	MPMovieControlStyle style = (MPMovieControlStyle)dAtoi(argv[4]);
	bool worked = playMovie(argv[1], argv[2], scale, style);
	return worked;
}

#endif //TORQUE_ALLOW_MOVIEPLAYER
#9
01/18/2012 (2:29 pm)
thank you very much Andrea :) you are very nice person