Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-29-2010, 11:56 AM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 118
Default Access the camera flash?

Hi. I can't figure out how to access the camera flash on the iPhone 4 for my app. Is anyone able to figure this out? I looked through the developer documentation and still can't figure it out. Thanks.

EDIT:When I try to import the <AVFoundation/AVFoundation.h, it's almost as if it doesn't detect it. For instance, I will type AVCaptureDevice in the .h file and it won't recognize it and give me an error. It detects that I've imported some classes (such as the ones that were in 3.0) but not the ones that are new to 4.0 (which one I need).

Last edited by Whitehk; 06-29-2010 at 02:33 PM. Reason: New Problem arised
Whitehk is offline   Reply With Quote
Old 06-29-2010, 01:06 PM   #2 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,578
Default

HAve you tried the AVCaptureDevice class? I don't have an iPhone4, so I can't try it, but it looks like the way to go.
JasonR is online now   Reply With Quote
Old 06-29-2010, 02:03 PM   #3 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 118
Default

Quote:
Originally Posted by JasonR View Post
HAve you tried the AVCaptureDevice class? I don't have an iPhone4, so I can't try it, but it looks like the way to go.
Yeah. That's the one I was looking at. I'm kind of a newbie and I'm trying to learn how to use the developer documentation. I just couldn't figure out how to implement it.
Whitehk is offline   Reply With Quote
Old 06-29-2010, 02:38 PM   #4 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,578
Default

Since you are doing something that's not standard, there's not going to be sample code. So your choice is either try it yourself, or wait until someone else does and posts it. (If you can wait 3-4 weeks, that's when I get my iPhone 4).

If I were you, I would play with the devices array, taking each element and setting flashMode until one of them turned on the flash. IT might look something like this:

Code:
NSArray *devices = [AVCaptureDevice devices];
for (AVCaptureDevice *device in devices) {
     device.flashMode = AVCaptureFlashModeOn;
}
Keep in mind, I'm guessing since I don't have the hardware, but see if that gets you started.
JasonR is online now   Reply With Quote
Old 06-29-2010, 02:42 PM   #5 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 118
Default

Quote:
Originally Posted by JasonR View Post
Since you are doing something that's not standard, there's not going to be sample code. So your choice is either try it yourself, or wait until someone else does and posts it. (If you can wait 3-4 weeks, that's when I get my iPhone 4).

If I were you, I would play with the devices array, taking each element and setting flashMode until one of them turned on the flash. IT might look something like this:

Code:
NSArray *devices = [AVCaptureDevice devices];
for (AVCaptureDevice *device in devices) {
     device.flashMode = AVCaptureFlashModeOn;
}
Keep in mind, I'm guessing since I don't have the hardware, but see if that gets you started.
Ok. I will look at it and see if I can get it working. Thanks for your help.
Whitehk is offline   Reply With Quote
Old 06-30-2010, 12:02 AM   #6 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 41
Default

Quote:
Originally Posted by Whitehk View Post
Ok. I will look at it and see if I can get it working. Thanks for your help.
Did you end up figuring it out?
ioiiooo is offline   Reply With Quote
Old 06-30-2010, 04:59 AM   #7 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

You need to take a look at AVCaptureSession, AVCaptureDevice, AVCaptureDeviceInput and AVCaptureVideoDataOutput in the documentation.

Everything you need is in there
harrytheshark is offline   Reply With Quote
Old 06-30-2010, 02:04 PM   #8 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 118
Default

Quote:
Originally Posted by ioiiooo View Post
Did you end up figuring it out?
Yes, I found that there was a WWDC session on iTunes over this, so I'm on my way to figuring it out.

Quote:
Originally Posted by harrytheshark
You need to take a look at AVCaptureSession, AVCaptureDevice, AVCaptureDeviceInput and AVCaptureVideoDataOutput in the documentation.

Everything you need is in there
Thanks. Like I said above, I figured this out when I watched the WWDC session.
Whitehk is offline   Reply With Quote
Old 06-30-2010, 02:16 PM   #9 (permalink)
Registered Member
 
thecyberduck's Avatar
 
Join Date: Jun 2010
Posts: 58
Default

tell us if you find something interesting
thecyberduck is offline   Reply With Quote
Old 07-05-2010, 12:16 AM   #10 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 1
Default

I'm having a very similar problem with trying to access the AVCaptureDevice class. I've imported the AVFoundation.framework into my project and also did an import statement of the AVFoundation/AVFoundation.h, but XCode just keeps saying AVCaptureDevice is undeclared. Also if I look at the header files within the framework itself, it's missing half of the 4.0 specific AVFoundation classes found within the documentation. Any ideas?

Andrew
mire3212 is offline   Reply With Quote
Old 07-08-2010, 02:27 AM   #11 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 3
Default

I have the same problem. Who can help us?

I found a free version of a iP4 flashlight:
LED-Light

Works perfect!
Tentoxa is offline   Reply With Quote
Old 07-09-2010, 01:17 AM   #12 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 2
Default

Quote:
Originally Posted by Whitehk View Post
Yes, I found that there was a WWDC session on iTunes over this, so I'm on my way to figuring it out.



Thanks. Like I said above, I figured this out when I watched the WWDC session.
Which session?
mikevh is offline   Reply With Quote
Old 07-09-2010, 04:28 AM   #13 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 1
Default

You try your codde on the real iphone 4?

I have one, and my code doesnt work in simulator, but in the iphone4, works....
tavobartientos is offline   Reply With Quote
Old 07-09-2010, 08:23 AM   #14 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 3
Default

@tavobartientos Could you show us your code, please? I have the same problem adding the AVFoundation.framework. (AVCaptureDevice is undeclared)

Last edited by Tentoxa; 07-09-2010 at 08:28 AM.
Tentoxa is offline   Reply With Quote
Old 07-10-2010, 06:31 AM   #15 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 1
Default

Before we can use the flashMode we must use lockForConfiguration.

My Code:


Code:
NSArray* devices = [AVCaptureDevice devices];
AVCaptureDevice* captDevice = nil;
NSLog(@"%d devices found", [devices count]);

for(AVCaptureDevice* device in devices)
{
	if([device hasFlash])
	{
		NSLog(@"device has flash");
		
		
		if ([device isFlashModeSupported:AVCaptureFlashModeOn]) {

			if([device lockForConfiguration:&error])
			{
				device.flashMode = AVCaptureFlashModeOn;
			}
			
		}
	}
}
}

But the flashlight is off :/ What´s wrong ?

Quote:
I have the same problem adding the AVFoundation.framework. (AVCaptureDevice is undeclared)
Test your Code on a iPhone 4. If you run this Code in Simulator it shows this error. If you use a Device it show´s no error

Last edited by darkangelbhv; 07-10-2010 at 07:31 AM.
darkangelbhv is offline   Reply With Quote
Old 07-10-2010, 01:42 PM   #16 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 36
Default Torch Mode

I too am having difficulties. I have tried the above methods and just get 2 errors. I came up with this but can't seem to get it to work.
Code:
	AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
	AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
	NSError *error = nil;
	AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
	
	if (videoInput) {
		[captureSession addInput:videoInput];
		
		AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
		[videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];
		
		[captureSession addOutput:videoOutput];
		
		[captureSession startRunning];
		
		videoCaptureDevice.torchMode = AVCaptureTorchModeOn;
	}
Any ideas?

EDIT:
It wont compile when building for the sim but builds and runs on my iPhone 4. When I tap the button with this action it returns SIGBART on it. telling me
Code:
videoCaptureDevice.torchMode = AVCaptureTorchModeOn;
this failed.

Last edited by MacBuddy; 07-10-2010 at 01:45 PM.
MacBuddy is offline   Reply With Quote
Old 07-10-2010, 02:25 PM   #17 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

You have to set an output as well as an input for this to work.
harrytheshark is offline   Reply With Quote
Old 07-10-2010, 03:18 PM   #18 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 36
Default

Quote:
Originally Posted by harrytheshark View Post
You have to set an output as well as an input for this to work.
what do you mean set an output and input?
MacBuddy is offline   Reply With Quote
Old 07-10-2010, 03:33 PM   #19 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Look in the documentation at AVCaptureSession, you should see an addOutput: method. Follow that through and you'll find what needs to be done
harrytheshark is offline   Reply With Quote
Old 07-10-2010, 04:08 PM   #20 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 36
Default

Quote:
Originally Posted by harrytheshark View Post
Look in the documentation at AVCaptureSession, you should see an addOutput: method. Follow that through and you'll find what needs to be done
Kay Thanks!
MacBuddy is offline   Reply With Quote
Old 07-10-2010, 04:32 PM   #21 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 36
Default

Quote:
Originally Posted by harrytheshark View Post
Look in the documentation at AVCaptureSession, you should see an addOutput: method. Follow that through and you'll find what needs to be done
Code:
		AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
		AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
		NSError *error = nil;
		AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
		if (videoInput) {
			[captureSession addInput:videoInput];
			
			AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
			[videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];
			
			[captureSession addOutput:videoOutput];
			
			[captureSession startRunning];
			
			videoCaptureDevice.torchMode = AVCaptureTorchModeOn;
		}
Output and Input are their
MacBuddy is offline   Reply With Quote
Old 07-10-2010, 05:00 PM   #22 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Sorry, I was talking more generally, in regards to your code, you need to run lockForConfiguration and unlockForConfiguration before and after changing device properties.
Code:
[videoCaptureDevice lockForConfiguration:nil];
videoCaptureDevice.torchMode = AVCaptureTorchModeOn;
[videoCaptureDevice unlockForConfiguration];
harrytheshark is offline   Reply With Quote
Old 07-11-2010, 11:34 PM   #23 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 59
Default Work yet?

Anyone get the LED flash to turn on yet?

I have tried all of these suggestions and got nothing to turn on. All of the calls work without errors and the flash modes are tuned on but nothing happens.

Any success for anyone?
crivoli is offline   Reply With Quote
Old 07-12-2010, 02:10 AM   #24 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Dhaka
Posts: 5
Send a message via Skype™ to agentBangla
Default

A question which is a little out of topic; do anyone know how can I set the video encoding format as H264 when recording video using AVCaptureSession, AVCaptureInput and AVCaptureOutput?
agentBangla is offline   Reply With Quote
Old 07-12-2010, 02:49 AM   #25 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Quote:
Originally Posted by crivoli View Post
Anyone get the LED flash to turn on yet?

I have tried all of these suggestions and got nothing to turn on. All of the calls work without errors and the flash modes are tuned on but nothing happens.

Any success for anyone?
Yeah, I've managed it.
harrytheshark is offline   Reply With Quote
Reply

Bookmarks

Tags
api, camera, camera flash, flash, framework

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 238
21 members and 217 guests
ADY, Alsahir, beleg_1998, Dani77, diyora, FAED, fredidf, iDifferent, iph_s, JamesCahall, JasonR, mer10, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, timle8n1, twerner
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:13 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0