我已经相当减少了这个问题,我希望得到一些帮助。
基本上这个类有两种方法,一种是开始录制音频(-recordMode
)而另一种是播放音频(playMode
)。我目前在一个带有单个视图控制器的项目中有这个类,其中有两个按钮调用相应的方法(rec,play)。没有其他变量,该类是自包含的。
然而,它不会播放/录制任何东西,我无法弄清楚为什么。当我尝试播放文件时,我得到的文件大小为0并且出现错误,因为当然无法使用AVAudioPlayer
引用初始化nil
。但我不明白为什么文件为空或为什么self.outputPath
是nil
。
.h文件
#import <AVFoundation/AVFoundation.h>
@interface MicCommunicator : NSObject<AVCaptureAudioDataOutputSampleBufferDelegate>
@property(nonatomic,retain) NSURL *outputPath;
@property(nonatomic,retain) AVCaptureSession * captureSession;
@property(nonatomic,retain) AVCaptureAudioDataOutput * output;
-(void)beginStreaming;
-(void)playMode;
-(void)recordMode;
@end
.m文件:
@implementation MicCommunicator {
AVAssetWriter *assetWriter;
AVAssetWriterInput *assetWriterInput;
}
@synthesize captureSession = _captureSession;
@synthesize output = _output;
@synthesize outputPath = _outputPath;
-(id)init {
if ((self = [super init])) {
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.outputPath = [NSURL fileURLWithPath:[[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"micOutput.output"]];
AudioChannelLayout acl;
bzero(&acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono; //kAudioChannelLayoutTag_Stereo;
NSDictionary *audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: kAudioFormatULaw],AVFormatIDKey,
[NSNumber numberWithFloat:8000.0],AVSampleRateKey,//was 44100.0
[NSData dataWithBytes: &acl length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
[NSNumber numberWithInt:1],AVNumberOfChannelsKey,
[NSNumber numberWithInt:8000.0],AVEncoderBitRateKey,
nil];
assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings];
[assetWriterInput setExpectsMediaDataInRealTime:YES];
assetWriter = [[AVAssetWriter assetWriterWithURL:_outputPath fileType:AVFileTypeWAVE error:nil] retain];
[assetWriter addInput:assetWriterInput];
}
return self;
}
-(void)dealloc {
[assetWriter release];
[super dealloc];
}
//conveniance methods
-(void)playMode
{
[self stopRecording];
NSError *error;
AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.outputPath error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil){
NSLog(@"error: %@",[error description]);
}else{
NSLog(@"playing");
[audioPlayer play];
}
}
-(void)recordMode
{
[self beginStreaming];
}
-(void)stopRecording
{
[self.captureSession stopRunning];
[assetWriterInput markAsFinished];
[assetWriter finishWriting];
NSDictionary *outputFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@",self.outputPath] error:nil];
NSLog (@"done. file size is %llu", [outputFileAttributes fileSize]);
}
//starts audio recording
-(void)beginStreaming {
self.captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
if (audioInput)
[self.captureSession addInput:audioInput];
else {
NSLog(@"No audio input found.");
return;
}
AVCaptureAudioDataOutput *output = [[AVCaptureAudioDataOutput alloc] init];
dispatch_queue_t outputQueue = dispatch_queue_create("micOutputDispatchQueue", NULL);
[output setSampleBufferDelegate:self queue:outputQueue];
dispatch_release(outputQueue);
[self.captureSession addOutput:output];
[assetWriter startWriting];
[self.captureSession startRunning];
}
//callback
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
AudioBufferList audioBufferList;
NSMutableData *data= [[NSMutableData alloc] init];
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
//for (int y = 0; y < audioBufferList.mNumberBuffers; y++) {
// AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
// Float32 *frame = (Float32*)audioBuffer.mData;
//
// [data appendBytes:frame length:audioBuffer.mDataByteSize];
//}
// append [data bytes] to your NSOutputStream
// These two lines write to disk, you may not need this, just providing an example
[assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
[assetWriterInput appendSampleBuffer:sampleBuffer];
CFRelease(blockBuffer);
blockBuffer=NULL;
[data release];
}
@end
答案 0 :(得分:0)
Per Apple Support:
因此,这就是错误 - 文件被创建,一些样本被成功写入,然后追加开始失败,原因不明。似乎AVAssetWriter仅在这些设置下失败。
AudioQueue应该用于ulaw音频