2010-04-20 58 views
6

這是我的方法:AVAudioRecorder不會記錄在設備

-(void) playOrRecord:(UIButton *)sender { 
    if (playBool == YES) { 
     NSError *error = nil; 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%d", [sender tag]] ofType:@"caf"]; 
     NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; 
     AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error]; 
     [player setNumberOfLoops:0]; 
     [player play];  
    } 
    else if (playBool == NO) { 
     if ([recorder isRecording]) { 
      [recorder stop]; 
      [nowRecording setImage:[UIImage imageNamed:@"NormalNormal.png"] forState:UIControlStateNormal]; 
      [nowRecording setImage:[UIImage imageNamed:@"NormalSelected.png"] forState:UIControlStateSelected]; 
     } 
     if (nowRecording == sender) { 
     nowRecording = nil; 
     return; 
     } 
     nowRecording = sender; 
     NSError *error = nil; 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%d", [sender tag]] ofType:@"caf"]; 
     NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; 
     [sender setImage:[UIImage imageNamed:@"RecordingNormal.png"] forState:UIControlStateNormal]; 
     [sender setImage:[UIImage imageNamed:@"RecordingSelected.png"] forState:UIControlStateSelected];   
     recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSettings error:&error]; 
     [recorder record]; 
    } 
} 

大部分是自我解釋; playBool是BOOL,當它處於播放模式時爲YES。 但是,當我在設備上運行它時,[記錄器記錄]返回NO。有沒有人有一個線索,爲什麼會發生這種情況?

回答

3

好的,解決了我自己的問題;我不知道爲什麼,但是我必須在設備上記錄到NSTemporaryDirectory。改變它完全解決了它。

+0

因爲你不能更改應用程序捆綁!大聲笑 – 2014-03-28 13:45:33

4

這是因爲您無法修改設備上的應用程序包(已簽名)。您可以記錄到您的應用的文檔文件夾或tmp目錄。

1

我有完全相同的問題。事實證明,我正在碰撞一條錯誤生成的路徑聲明。 AVAudioRecorder初始化正常 - 沒有發生 - 但是iOS根本不允許我寫信給它。爲什麼?檢查出來:

/* The following code will fail - why? It fails because it is trying to write some path that is NOT accessible by 
    this app. 
NSString *recordedAudioPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
           objectAtIndex:0] 
           stringByAppendingString:@"recorded.caf"]; 
*/ 
// Correction: 
NSString *recordedAudioPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
           objectAtIndex:0]; 

recordedAudioPath = [recordedAudioPath stringByAppendingPathComponent:@"recorded.caf"]; 
NSURL *recordURL = [NSURL fileURLWithPath:recordedAudioPath]; 

這使現在完全合理。感謝您指引正確的方向。

+0

他們看起來完全一樣... – Gu1234 2011-05-08 21:22:41

+0

區別在於如果需要,路徑組件被添加。它本質上增加了一個/ recorded.caf 之前,如果他用以前的 '不正確' 的代碼會工作: 的NSString * recordedAudioPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0] stringByAppendingString:@「/ recorded.caf「]; – 2014-09-17 14:37:50

17

如果在你的代碼的任何地方,你有這樣的設置:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 

將其更改爲:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

設置錯誤AVAudioSession類別將導致錄音/播放失敗。

1

此外,請確保您的應用程序被允許訪問的麥克風(即iOS的設置,然後找到你的應用程序,它應該有「麥克風」,你需要切換到「開」)

+0

什麼?我一直試圖調試兩天的這個問題不可能如此簡單,當然這個設備具有麥克風權限,請參閱... 哦,就是這樣。設備/模擬器完全是一隻紅鯡魚。 iOS至少應該記錄一個錯誤,而不是默默地失敗。 謝謝,有時它是如此基本的東西。 – 2018-01-12 19:49:51