// Super simple movie player for playing mp3

#include <Carbon/Carbon.h> // for a lot of core stuff
#include <QuickTime/Movies.h> // for QT
#include <QuickTime/QuickTimeComponents.h> // for QT


OSErr GetResourceFSRef(CFStringRef fileName, FSRef *theRef)
{
	CFURLRef url;
	char success;
	
	url = CFBundleCopyResourceURL( CFBundleGetMainBundle(), fileName, NULL, NULL );
	if (url == NULL) return paramErr;
	success = CFURLGetFSRef( url, theRef );
	if (!success) return fnfErr;
	return noErr;
}


Movie	theMovie;

void TimerAction (EventLoopTimerRef theTimer, void *userData)
{
	if (!IsMovieDone(theMovie))
		MoviesTask(NULL,0);
}

void InstallTimer()
{
	EventLoopTimerUPP	timerUPP;
	EventLoopTimerRef	theTimer;
	
	timerUPP = NewEventLoopTimerUPP(TimerAction);
	InstallEventLoopTimer (GetMainEventLoop(), kEventDurationSecond/10,
			kEventDurationSecond/10, timerUPP, NULL, theTimer);
}


int main(int argc, char* argv[])
{
	FSRef theRef;
	Handle	dataRef;
	OSType	dataRefType;
	OSErr	theErr;
	
	theErr = EnterMovies();
	if (theErr != noErr)
		printf("EnterMovies %d\n", theErr);
	
	theErr = GetResourceFSRef(CFSTR("Ljud liksom.m4a"), &theRef);
	if (theErr != noErr)
		printf("GetResourceFSRef %d\n", theErr);
	theErr = QTNewDataReferenceFromFSRef(&theRef, 0, &dataRef, &dataRefType);
	if (theErr != noErr)
		printf("QTNewDataReferenceFromFSRef %d\n", theErr);
	
	theErr = NewMovieFromDataRef(&theMovie, newMovieActive, NULL, dataRef, dataRefType);
	if (theErr != noErr)
		printf("NewMovieFromDataRef %d\n", theErr);
	
	// Try playing it
	StartMovie(theMovie);
	sleep(1); // Must do this! (Why?)
	
	InstallTimer();
	RunApplicationEventLoop();
	
	DisposeMovie(theMovie);
	
	ExitMovies();
	return 0;
}
