我正在使用AVAudioRecorder在我的iOS应用程序中录制内容,我希望以字节为单位获取录制文件的大小。我有办法做到吗?
这是我设置录像机的代码:
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
这是我尝试获取文件大小的地方:
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
// Convert to NSData
NSData *data = [NSData dataWithContentsOfFile:recorder.url];
// Convert to NSString
NSString* dataAsString = [NSString stringWithUTF8String:[data bytes]];
NSLog(@"Size: %@", dataAsString);
}
NSLog不会返回任何内容。任何帮助表示赞赏。
答案 0 :(得分:2)
好的,所以我想通了,你可以通过这样做来获得文件大小:
unsigned long long size = [[NSFileManager defaultManager] attributesOfItemAtPath:[recorder.url path] error:nil].fileSize;
然后通过执行以下操作将其转换为字符串:
NSLog(@"This is the file size of the recording in bytes: %llu", size);