保存和加载文件的代码适用于EMU,但不适用于设备

时间:2010-10-05 15:29:16

标签: iphone objective-c

我正在使用一些代码来录制和保存一些音频。然后再玩。

它在模拟器上运行良好,但在设备上文件的大小为0。

我想也许有些东西我不知道。

这是我保存文件并从中加载的路径。

在电话上:/var/mobile/Applications/8CE3099A-0CA6-411F-B197-B57699076E8F/Documents/soundfile.caf

在模拟器上:/ Users / johny / Library / Application Support / iPhone Simulator / 4.0.2 / Applications / 8BE98F55-250D-49EC-B1C0-C2C4D48B4CCE / Documents / soundfile.caf

我用来制作路径的代码是:

NSString* fileName = [[self  
 documentsPath]stringByAppendingPathComponent:@"soundfile.caf"];

加载音频文件的方法。这似乎工作正常。至少它会显示任何调试消息。

-(AudioFileID)openAudioFile:(NSString*)filePath
{
NSLog(@"openAudioFile");
AudioFileID outAFID;
// use the NSURl instead of a cfurlref cuz it is easier
NSURL * afUrl = [NSURL fileURLWithPath:filePath];
OSStatus result = AudioFileOpenURL((CFURLRef)afUrl, kAudioFileReadPermission, 0,  
 &outAFID); 
if (result != 0) NSLog(@"cannot open file: %@",filePath);
return outAFID;
}

-(UInt32)audioFileSize:(AudioFileID)fileDescriptor
{
NSLog(@"audioFileSize");
UInt64 outDataSize = 0;
UInt32 thePropSize = sizeof(UInt64);
OSStatus result = AudioFileGetProperty(fileDescriptor, 
    kAudioFilePropertyAudioDataByteCount, &thePropSize, &outDataSize);
if(result != 0) NSLog(@"cannot find file size");
return (UInt32)outDataSize;
}

我看到的问题是filesize是0.因为这两种方法都有效。当我在NSLog中打印出文件大小时,它是0.

我创建AudioRecorder的方法

- (NSError*) createAVAudioRecorder 
{
NSLog(@"creatAVAudioRecorder");

[audioRecorder release];
audioRecorder = nil;

NSString *destinationString = [[self 
documentsPath]stringByAppendingPathComponent:@"soundfile.caf"];

NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];

NSMutableDictionary *recordSettings =[[NSMutableDictionary alloc] 
initWithCapacity:10];

[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: 
AVFormatIDKey];
float sampleRate = 44100; // 22050
[recordSettings setObject:[NSNumber numberWithFloat:sampleRate] forKey: 
AVSampleRateKey];
int NumChannels = 2; //stero or mono
[recordSettings setObject:[NSNumber 
numberWithInt:NumChannels]forKey:AVNumberOfChannelsKey];
int bitDepth = 16;
[recordSettings setObject:[NSNumber numberWithInt:bitDepth] 
forKey:AVLinearPCMBitDepthKey];
bool bigEndian = NO;
[recordSettings setObject:[NSNumber 
numberWithBool:bigEndian]forKey:AVLinearPCMIsBigEndianKey];
bool floatingSamples = NO;
[recordSettings setObject:[NSNumber 
numberWithBool:floatingSamples]forKey:AVLinearPCMIsFloatKey];


NSError *recorderSetupError = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationURL 
settings:recordSettings error:&recorderSetupError]; 
[recordSettings release];

if (recorderSetupError) 
{
    NSLog(@"Record Setup Error");
    UIAlertView *cantRecordAlert = [[UIAlertView alloc] initWithTitle: @"Can't 
 record" message: [recorderSetupError localizedDescription]delegate: nil 
 cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [cantRecordAlert show];
    [cantRecordAlert release];
    return recorderSetupError;
}
[audioRecorder prepareToRecord];

audioRecorder.delegate = self;

return recorderSetupError;
}

我将这些方法称为开始和停止录制

-(IBAction) startRecording
{
[self createAVAudioRecorder ];
if (! [self alertIfNoAudioInput]) 
    return;
[audioRecorder record];
}
-(IBAction) stopRecording
{
NSLog(@"stopRecording Method");
[audioRecorder stop];

}

非常感谢, -code

0 个答案:

没有答案