1。添加头文件,并添加相应的framework
#import<CoreMedia/CoreMedia.h>
#import<AVFoundation/AVFoundation.h>
2。添加 AVCaptureAudioDataOutputSampleBufferDelegate
3。初始化并开始获取声音
AVCaptureSession*captureSession;
-(void)initVoice
{
if(captureSession)
{
[captureSessionstartRunning];
}
else
{
captureSession= [[AVCaptureSessionalloc]init];
AVCaptureDevice*audioDevice = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeAudio];
if(audioDevice) {
NSError*error;
AVCaptureDeviceInput*audioIn = [AVCaptureDeviceInputdeviceInputWithDevice:audioDeviceerror:&error];
if ( !error ) {
if ([captureSession canAddInput:audioIn])
[captureSession addInput:audioIn];
else
NSLog(@"Couldn't add audio input");
}
else
NSLog(@"Couldn't create audio input");
}
else
NSLog(@"Couldn't create audio capture device");
AVCaptureAudioDataOutput*audioOut = [[AVCaptureAudioDataOutputalloc]init];
[audioOutsetSampleBufferDelegate:selfqueue:dispatch_get_main_queue()];
if ([captureSession canAddOutput:audioOut]) {
[captureSession addOutput:audioOut];
//audioConnection =
[audioOutconnectionWithMediaType:AVMediaTypeAudio];
}
else
NSLog(@"Couldn't add audio output");
[audioOut release];
[captureSessionstartRunning];
}
}
附件:需要用到的AVCaptureAudioDataOutput
#define SINT16_MAX 32767.0;
@interface AVCaptureOutput (AVCaptureOutputUtilities)
- (AVCaptureConnection *)connectionWithMediaType:(NSString *)mediaType;
@end
@implementation AVCaptureOutput (AVCaptureOutputUtilities)
- (AVCaptureConnection *)connectionWithMediaType:(NSString *)mediaType {
for(AVCaptureConnection*connectioninself.connections) {
for ( AVCaptureInputPort *port in [connection inputPorts] ) {
if ( [[port mediaType] isEqual:mediaType] ) {
return [[connection retain] autorelease];
}
}
}
returnnil;
}
@end
分享到:
相关推荐
完美解决IPHONE 4S无外声音或话筒无声
iphone4 系统声音iphone4 系统声音iphone4 系统声音iphone4 系统声音
iPhone播放声音的例子iPhone播放声音的例子iPhone播放声音的例子iPhone播放声音的例子
iphone键盘水滴声音,非常好听。我找了好久
iphone 声音资源iphone 声音资源iphone 声音资源iphone 声音资源
锁屏苹果 iPhone4的 修改 锁屏音效
锁屏声音IPHONE最最最NB的声音
Iphone的caf格式系统声音: 代码:NSString *path = [[NSBundle mainBundle] pathForResource:@"SIMToolkitPositiveACK" ofType:@"caf"]; AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:...
利用iphone提供的声音编程接口进行声音播放
iphone 建议的声音文件格式,用这个格式更加省电。呵呵呵。
photoShutter2 iPhone相机拍照声音反向波音频文件
iPhone最牛锁屏音效,已转为高清晰的oggAndroid可用音效,可以直接替换Android锁屏音效
iPhone中使用qq视频聊天播放的声音,已经转为mp3格式,包括其它使用iPhone qq时的所有声音都转换了,有喜欢的可以作为短信铃声,还是挺好听的。
iphone X iphone 8 原型框 iphone X iphone 8 原型框
iPhone狂:约会iPhone
一个练习需要的声音 一个练习需要的声音
自定义声音波纹,适合作录音应用的开发,模仿iPhone录音效果
Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码...
iphoneos-arm iPhone 必备工具