2014-04-25 40 views
3

我面臨AVFoundation框架的問題。我的應用程序都準備在舊版本,然後我升級到ios 7這次我的錄音機在模擬器上工作,但不能在iphone上工作。所以任何人都可以幫助我解決實際問題。音頻記錄器不能在iPhone與iPhone 7

我的代碼是這樣的

-(IBAction)startRecording 
{ 
    btnRecord.showsTouchWhenHighlighted = YES; 
    btnRecord.enabled = NO; 
    btnPlay.enabled = NO; 
    btnStop.enabled = YES; 
    btnPause.enabled = YES; 
    btnDelete.enabled = NO; 
    if(!flagPause) 
    { 
     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
     [dateFormat setDateFormat:@"yyyy-MM-dd"]; 

     NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; 
     [timeFormat setDateFormat:@"HH.mm.SS"]; 

     NSDate *now = [[NSDate alloc] init]; 

     theDate = [dateFormat stringFromDate:now]; 
     theTime = [timeFormat stringFromDate:now]; 


     NSString *filename =[NSString stringWithFormat:@"|%@|%@|",theDate,theTime]; 
     theDate=[[NSString alloc]initWithString:filename]; 


     AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
     [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; 

     ***** i try this one but not working 
     //[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 
     //[audioSession setActive:YES error:nil]; 


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

     if(recordEncoding == ENC_PCM) 
     { 
      [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey]; 
      [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey]; 
      [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 
      [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
      [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 
      [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];  
     } 
     else 
     { 
      NSNumber *formatObject; 
      switch (recordEncoding) 
      { 
       case (ENC_AAC): 
        formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC]; 
        break; 
       case (ENC_ALAC): 
        formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless]; 
        break; 
       case (ENC_IMA4): 
        formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4]; 
        break; 
       case (ENC_ILBC): 
        formatObject = [NSNumber numberWithInt: kAudioFormatiLBC]; 
        break; 
       case (ENC_ULAW): 
        formatObject = [NSNumber numberWithInt: kAudioFormatULaw]; 
        break; 
       default: 
        formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4]; 
      } 
      [recordSettings setObject:formatObject forKey: AVFormatIDKey]; 
      [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey]; 
      [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 
      [recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey]; 

      //**************** I try this one ***********************>>>> 
      //[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey]; 

      [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 

      [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey]; 
     } 
     NSURL *url; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
     NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Recording"]; 
     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
      [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 

     NSString *filePath =[NSString stringWithFormat:@"%@/%@.caf",dataPath,theDate]; 
     // appiPhone.strATitle = theDate; 
     url = [NSURL fileURLWithPath:filePath]; 
     NSError *error = nil; 

     audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error]; 

     if ([audioRecorder prepareToRecord] == YES) 
     { 
      [audioRecorder record]; 
     } 
     else 
     { 
      //int errorCode = CFSwapInt32HostToBig ([error code]); 
      // NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);  
     } 
    } 
    else 
    { 
     [audioRecorder record]; 
     flagPause = NO; 
    } 
    //appiPhone.intBtnAnsTag = 1; 
} 
+0

這不是這個問題的答案,但按我的觀察,你已經設置AVNumberOfChannelsKey爲2。你能明確其需求?否則,它可以保持1.它會增加錄製的質量。 – Yogi

+0

你不能聽聲音,但可能它記錄..如果它可以在模擬器上工作,嘗試聆聽你錄製的聲音到iPhone耳機揚聲器.. –

+0

6和7之間的區別之一是需要權限才能訪問麥克風。檢查您已授予應用程序的權限。查看設置 - >隱私 - >麥克風 – MDB983

回答

0

我最近開發了一個非常簡單的播放和錄製演示(真的只是3個按鈕做你所期望的)。我最初有同樣的問題:它會在iOS模擬器中工作,但不在我的設備上。首先,我必須確保應用程序必須通過將設置使用麥克風的權限 - >隱私 - >麥克風

我也有過建立我的錄音機

AVAudioSession *session = [AVAudioSession sharedInstance]; 
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 
[session setActive:YES error:nil]; 

下面前將以下代碼是我超級簡單演示的完整代碼。 AudioPlayer和AudioRecorder被召喚出來的.H

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    AVAudioSession *session = [AVAudioSession sharedInstance]; 
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 
    [session setActive:YES error:nil]; 
    NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:44100],AVSampleRateKey,[NSNumber numberWithInt:kAudioFormatAppleLossless], AVFormatIDKey, [NSNumber numberWithInt:1],AVNumberOfChannelsKey, [NSNumber numberWithInt:AVAudioQualityMedium], AVEncoderAudioQualityKey, nil]; 

    NSURL *audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"audioRecording.m4a"]]; 

    self.audioRecorder = [[AVAudioRecorder alloc]initWithURL:audioFileURL settings:audioSettings error:nil]; 

    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)audioRecord:(id)sender { 

    [self.audioRecorder record]; 
} 

- (IBAction)audioStop:(id)sender { 
    [self.audioPlayer stop]; 
    [self.audioRecorder stop]; 

    NSURL *audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"audioRecording.m4a"]]; 
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil]; 
} 

- (IBAction)audioPlay:(id)sender { 

    [self.audioPlayer play]; 
} 
@end