Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CocoaBean

macrumors newbie
Original poster
Feb 23, 2009
25
0
Hi,

I recently came across some code for creating a simple built in iSight app which displays a view using the iSight camera on the macbook. I thought I would play around and see if I could add features to the basic app to make it more interesting.
The code was all in the -awakeFromNib method. I have created a button and changed the method to -viewButton which is called when the user presses the button (the session then starts).

I would like to add a stop button but am slightly confused with the code required for my -stopButton method.
The QTkit is used (<QTkit/QTkit.h>)

The code I have is here:

iSight.h

Code:
#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>


@interface iSight : NSObject {
	IBOutlet QTCaptureView *outputView;
	
}

-(IBAction)viewButton:(id)sender;
-(IBAction)stopButton:(id)sender;

@end

iSight.m

Code:
#import "iSight.h"

@implementation iSight

-(IBAction)viewButton:(id)sender
{
	//Create the QT capture session
	QTCaptureSession *session = [[QTCaptureSession alloc] init];
	/* Select the default Video input device */
	QTCaptureDevice *iSight = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
	
	/* Passing nil for the NSError parameter may not be the best idea
	 but i will leave error handling up to you */
	[iSight open:nil];
	/* Create a QTKit input for the session using the iSight Device */
	QTCaptureDeviceInput *myInput = [QTCaptureDeviceInput deviceInputWithDevice:iSight];
	
	/* Create a capture session for the live vidwo and add inputs get the ball rolling etc */
	[session addInput:myInput error:nil];
	[outputView setCaptureSession:session];
	
	/* Let the video madness begin */
	[session startRunning]; 
}

-(IBAction)stopButton:(id)sender
{
	//[session stopRunning]; error
}


@end

I have commented out the code under the stopButton Method as it doesn't work (I kind of expected that but don't know how to approach it).
The idea is to stop the current session when the stop button is clicked.

I can't remember where the original code was from but it was a good example.
 

timbos

macrumors member
Jan 2, 2009
34
0
session is defined within your viewButton: method, so the stopButton: method can't see it. What if you define session in the header file to make it a global.
 

CocoaBean

macrumors newbie
Original poster
Feb 23, 2009
25
0
Of course - it now works, thanks.

Just a day of complete brain fog!!

I'll play around with it more and if I have any problems, I'll post back :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.