免责声明是AVAudioRecorder的新手
我在做什么我正在使用iPhone麦克风录制声音的应用程序上工作。录制完声音后,我需要将声音(应该是AVAsset,对吗?)转换为NSData以发送到我们的后端。
问题是什么问题是我不确定如何“获取”应该通过AVAudioRecorder录制的音频。 AVAudioRecorder有一个称为- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
的委托(delegate)方法。我本来希望包含从此委托(delegate)方法传递的音频的实际AVAsset,但事实并非如此。它给我的是带有aRecorder
属性的.url
对象。当我NSLog从传递的aRecorder
的URL时,它显示出来。实际上,我可以在下面的代码中NSLog文件的长度:
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
{
DLog (@"audioRecorderDidFinishRecording:successfully: %@",aRecorder);
AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:aRecorder.url options:nil];
CMTime audioDuration = audioAsset.duration;
float audioDurationSeconds = CMTimeGetSeconds(audioDuration);
NSLog(@"asset length = %f", audioDurationSeconds); //Logs 7.051 seconds, so I know it's "there".
self.audioURL = aRecorder.url;
}
问题当我将
self.audioURL
传递到下一个viewController的self.mediaURL
并尝试从AssetLibrary抓取文件时(与之前的操作类似), Assets 不会从AssetLibrary返回(即使当我po self.mediaURL
时,它确实记录了正确的url) :if (self.mediaURL) {
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:self.mediaURL resultBlock:^(ALAsset *asset) {
if (asset) {
// This block does NOT get called...
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc((long)rep.size);
NSUInteger buffered =[rep getBytes:buffer fromOffset:0.0 length:(long)rep.size error:nil];
NSMutableData *body = [[NSMutableData alloc] init];
body = [NSMutableData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
[dataToSendToServer setObject:body forKey:@"audioData"];
}
} failureBlock:^(NSError *error) {
NSLog(@"FAILED TO ACCESS AUDIO FROM URL: %@!", self.mediaURL);
}];
}
else {
NSLog(@"NO AUDIO DATA!");
}
}
因为我是AVAudioRecorder的新手,所以也许我只是没有正确设计此流程。任何人都可以帮助我获得实际的音频数据。
谢谢!
最佳答案
AVAudioRecorder记录到文件,而不是 Assets 库。
因此,您可以简单地从该文件中读取数据。
关于ios - iOS:从AVAudioRecorder获取音频,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33089971/