2010-02-18 49 views
0

我正在編程一款應用程序,可以讓我錄製門鈴聲音,並在用戶按下按鈕時看到的門鈴上播放。 我的應用程序似乎記錄並保存文件(根據iPhone模擬器,文件在那裏(在文檔中),因爲我在應用程序委託中創建了目錄))。 但是播放不起作用。 (順便說一下,錄製屬性返回TRUE。)爲什麼我的錄音不能播放? (iPhone SDK)

UPDATE:這裏是the zip of the whole project

有人能幫我看看有什麼問題嗎?

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.view.layer.cornerRadius = 8; 
    [actionButton setBackgroundImage:[[UIImage imageNamed:@"biggreenbutton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal]; 
    NSFileManager *nsf = [NSFileManager defaultManager]; 
    NSArray *allDocs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [allDocs objectAtIndex:0]; 
    NSString *savedDBells = [path stringByAppendingPathComponent:@"Custom Doorbells"]; 
    NSString *nextFile; 
    for (NSInteger i = 1; i < 100; i++){ 
     if (![nsf fileExistsAtPath:[savedDBells stringByAppendingPathComponent:[NSString stringWithFormat:@"d%i%@",i,@".caf"]]]){ 
      nextFile = [savedDBells stringByAppendingPathComponent:[NSString stringWithFormat:@"d%i%@",i,@".caf"]]; 
      break; 
     } 
    } 
    NSURL *url = [[NSURL alloc] initFileURLWithPath:nextFile]; 
    NSDictionary *setup = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey, 
          [NSNumber numberWithInt:44100.0],AVSampleRateKey, 
          [NSNumber numberWithInt: 2],AVNumberOfChannelsKey, 
          [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, 
          [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey, 
          [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey, 
          nil]; 

    NSError *err; 
    NSLog(@"%@",url); 
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:setup error:&err]; 
    [setup release]; 
    [url release]; 
    //Gradient code 
    /* 
    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = self.view.bounds; 
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil]; 
    [self.view.layer insertSublayer:gradient atIndex:0]; 
    */ 
} 

-(IBAction) actionButtonPressed: (id) sender{ 
    NSLog(@"%@",actionButton.titleLabel.text); 
    if ([actionButton.titleLabel.text isEqual:@"Finish Recording"]){ 
     BOOL test = recorder.recording; 
     NSLog(@"Recording property = %i",test); 
     [upCounter invalidate]; 
     [recorder stop]; 

     [self dismissModalViewControllerAnimated:YES]; 
    } 
    if ([actionButton.titleLabel.text isEqual:@"Begin Recording"]){ 
     upCounter = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; 
     [[NSRunLoop mainRunLoop] addTimer:upCounter forMode:NSDefaultRunLoopMode]; 
     [actionButton setTitle:@"Finish Recording" forState:UIControlStateNormal]; 
     [actionButton setBackgroundImage:[[UIImage imageNamed:@"bigredbutton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal]; 
     NSLog(@"Recorder record = %i",[recorder record]); 
    } 

} 

在另一個視圖控制器:

-(IBAction) recordingButtonPressed:(id) sender{ 
    AVAudioSession *avs = [AVAudioSession sharedInstance]; 
    //Test for written folder 
    /*NSFileManager *fileMan = [NSFileManager defaultManager]; 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    */ 

    if (avs.inputIsAvailable){ 
     NSString *recordedFile; 

     RecordingViewController *recorderVC = [[RecordingViewController alloc] initWithNibName:@"RecordingViewController" bundle:nil]; 
     recorderVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
     [self presentModalViewController:recorderVC animated:YES]; 
     recordedFile = [recorderVC.recorder.url absoluteString]; 
     [recorderVC release]; 
     self.currentSoundPath = recordedFile; 
     NSLog(@"%@",currentSoundPath); 

     /*if ([fileMan fileExistsAtPath:recordedFile]){ 
     UIActionSheet *overwrite = [[UIActionSheet alloc] initWithTitle:@"Overwrite the current doorbell recording?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Overwrite" otherButtonTitles:nil]; 
     overwrite.delegate = self; 
     [overwrite showInView:self.view]; 
     [overwrite release]; 
     }*/ 

     /*NSDictionary *recorderSettingsDict=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey, 
              [NSNumber numberWithInt:44100.0],AVSampleRateKey, 
              [NSNumber numberWithInt: 2],AVNumberOfChannelsKey, 
              [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, 
              [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey, 
              [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey, 
              nil]; 
     AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:recordedFile settings:<#(NSDictionary *)settings#> error:<#(NSError **)outError#>];*/ 
    } 
    else{ 
     UIAlertView *noAudio = [[UIAlertView alloc] initWithTitle:@"No microphone connected" message:@"Please connect a microphone to record a doorbell." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [noAudio show]; 
     [noAudio release]; 
    } 
} 
+0

這完全讓我想起了那門鈴SNL skit。 – 2010-02-18 21:48:29

+0

shanooz ....大聲笑 – 2010-11-20 04:57:19

回答

1

,在跳出我的唯一的事情是,你調用[NSNumber的numberWithInt:44100.0]代替numberWithDouble。

1

雖然這段代碼可能不是問題,但在我的情況下,無法播放錄製的音頻文件是因爲未調用[audioRecorder stop]。當我的音頻播放器參考代碼到達應該調用stop的代碼時,我已經安裝了代碼,因此請使用調試器確保何時告訴錄製停止,您仍然可以連接到錄音機。

相關問題