2012-03-01 104 views
0

我想製作一個錄音應用程序,所以顯然我需要能夠錄製和播放音頻文件。它看起來像在工作,根本沒有錯誤,但它不會工作。我不確定它實際上是在錄音嗎?iPhone不會播放音頻文件

它應該創建聲音文件本身,對不對?我不需要在項目中輸入完善的文件嗎?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    playButton.enabled = NO; 
    stopButton.enabled = NO; 

    NSArray *dirPaths; 
    NSString *docsDir; 

    dirPaths = NSSearchPathForDirectoriesInDomains(
                NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0]; 
    NSString *soundFilePath = [docsDir 
           stringByAppendingPathComponent:@"sound.caf"]; 

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

    NSDictionary *recordSettings = [NSDictionary 
            dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithInt:AVAudioQualityMin], 
            AVEncoderAudioQualityKey, 
            [NSNumber numberWithInt:16], 
            AVEncoderBitRateKey, 
            [NSNumber numberWithInt: 2], 
            AVNumberOfChannelsKey, 
            [NSNumber numberWithFloat:44100.0], 
            AVSampleRateKey, 
            nil]; 

    NSError *error = nil; 

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

    if (error) 
    { 
     NSLog(@"error: %@", [error localizedDescription]); 

    } else { 
     [audioRecorder prepareToRecord]; 
    } 

} 

// Methods called by the start recording button, stop recording, and play. 
-(void) recordAudio 
{ 
    if (!audioRecorder.recording) 
    { 
     playButton.enabled = NO; 
     stopButton.enabled = YES; 
     [audioRecorder record]; 
    } 
} 
-(void)stop 
{ 
    stopButton.enabled = NO; 
    playButton.enabled = YES; 
    recordButton.enabled = YES; 

    if (audioRecorder.recording) 
    { 
     [audioRecorder stop]; 
    } else if (audioPlayer.playing) { 
     [audioPlayer stop]; 
    } 
} 
-(void) playAudio 
{ 
    if (!audioRecorder.recording) 
    { 
     stopButton.enabled = YES; 
     recordButton.enabled = NO; 

     NSError *error; 

     audioPlayer = [[AVAudioPlayer alloc] 
         initWithContentsOfURL:audioRecorder.url          
         error:&error]; 

     audioPlayer.delegate = self; 

     if (error) 
      NSLog(@"Error: %@", 
        [error localizedDescription]); 
     else { 
      [audioPlayer setVolume:1.0]; 
      [audioPlayer play]; 
     } 
    } 
} 

// Delegate methods 

-(void)audioPlayerDidFinishPlaying: 
(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    recordButton.enabled = YES; 
    stopButton.enabled = NO; 

    NSLog(@"did play"); 
} 
-(void)audioPlayerDecodeErrorDidOccur: 
(AVAudioPlayer *)player 
           error:(NSError *)error 
{ 
    NSLog(@"Decode Error occurred"); 
} 
-(void)audioRecorderDidFinishRecording: 
(AVAudioRecorder *)recorder 
          successfully:(BOOL)flag 
{ 
    NSLog(@"did record"); 
} 
-(void)audioRecorderEncodeErrorDidOccur: 
(AVAudioRecorder *)recorder 
            error:(NSError *)error 
{ 
    NSLog(@"Encode Error occurred"); 
} 

提前致謝。我對整個錄音工作很陌生,並且處理音頻文件和目錄。

+1

您剛纔複製並粘貼http://www.techotopia.com/index.php/Recording_Audio_on_an_iPhone_with_AVAudioRecorder_%28iOS_4%29 – 2012-03-01 12:35:48

+0

是我沒有 - 當學習新的概念,我傾向於複製粘貼一些預定義的東西,讓它工作,攪拌它並看看會發生什麼,然後從頭開始創建我自己 - 每個人都學習不同的東西,這對我很有用 - 所以我得首先得到這個工作:) – Seerex 2012-03-01 13:02:47

+0

我只是問,那個問題是如果它從一開始就不工作,你不知道什麼是它的工作。 – 2012-03-01 13:30:22

回答

0

嘗試增加AVAudioSession代碼如下..

NSError *error; 

AVAudioSession * audioSession = [AVAudioSession sharedInstance]; 

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error]; 

[audioSession setActive:YES error: &error];