我在我的應用中實現了音頻錄製,我試過了我在模擬器中編寫的代碼,並且所有工作都很好,但是當我在實際設備中嘗試時我檢查音頻記錄持續時間,它始終爲零,但在模擬器中,它顯示正確的持續時間和代碼是相同的。我必須設置一些在實際設備中使用記錄?在真實設備中錄製音頻不起作用
0
A
回答
1
我有一個很好的記錄方法:
-(IBAction) startRecording
{ progressView.progress = 0.0;
NSLog(@"startRecording");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
recordSetting = [[NSMutableDictionary alloc] init];
// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
// These settings are used if we are using kAudioFormatLinearPCM format
//[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
//[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
// Create a new dated file
//NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
// NSString *caldate = [now description];
// recorderFilePath = [[NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate] retain];
recorderFilePath = [NSString stringWithFormat:@"%@/MySound.caf", DOCUMENTS_FOLDER] ;
NSLog(@"recorderFilePath: %@",recorderFilePath);
_recorderFilePath = recorderFilePath;
self.recorderFilePath = recorderFilePath;
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(audioData)
{
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtPath:[url path] error:&err];
}
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder){
NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
return;
}
//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: @"Audio input hardware not available"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
return;
}
// start recording
[recorder recordForDuration:(NSTimeInterval) 30];
lblStatusMsg.text = @"Recording...";
progressView.progress = 0.0;
timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
NSLog(@"recording");
startRecord.enabled = NO;
stopRecord.enabled = YES;
}
+0
你已經嘗試過這種方法在一個真正的iPad,它的工作原理? – user1575803 2013-04-23 12:13:24
+0
是的,我的功能完全正常 – Mutawe 2013-04-23 12:36:34
+0
我有同樣的問題,在錄製音頻時出現問題,但是當我在模擬器中嘗試它時, – user1575803 2013-04-23 13:17:23
相關問題
- 1. 聲音在設備中不起作用
- 2. loadUrl在真實設備中不起作用
- 3. Fucntion drawImage在真實設備上不起作用
- 4. 使用藍牙設備在AVCaptureSession中錄製音頻
- 5. 如何從默認音頻設備錄製和播放音頻
- 6. CustomListview與圖像齊射不起作用的真實設備
- 7. 在音頻回調中錄製音頻
- 8. 在visual studio中錄製音頻(在網站開發中)不起作用
- 9. 音頻FLVPlayback不起作用
- 10. 嵌入在UIWebView中的Youtube視頻在真實設備上沒有聲音
- 11. 通過設備攝像頭錄製視頻和音頻
- 12. 如何啓用/禁用錄製音頻設備?
- 13. CMMotionManager:設備校準在實際設備上不起作用
- 14. 真實設備
- 15. 真實設備
- 16. 在IOS7中的DDASLLogger在真實設備上不記錄
- 17. 不能真實設備
- 18. 有沒有辦法在iPhone上錄製設備音頻?
- 19. 在Swift中錄製音頻
- 20. 在flash中錄製音頻
- 21. 在OGG中錄製音頻
- 22. 在Android中錄製音頻
- 23. 在Android中錄製音頻
- 24. 在MVC3中錄製音頻
- 25. 在Android中錄製音頻
- 26. Iphone音頻只在模擬器中工作不在設備
- 27. HSLab:null音頻設備
- 28. HTML5視頻標記在移動設備中不起作用
- 29. 記錄directshow音頻設備到文件
- 30. Android設備之間的實時音頻
是你能找出爲什麼發生這種情況? 。面對同樣的問題 – vijar 2014-07-16 21:50:11