2012-02-22 11 views
1

このトピックに関するすべての種類の投稿を閲覧しましたが、回答が役に立たないだけです。私はthisチュートリアルを使用して、オーディオファイルの録音と再生を実装しました。見逃していると思われるのは、レコードを永久に保存する方法です。私のアプリを終了すると、サウンドファイルはありますが、そこには何もありません。私はそれがrocerdを保存しているのか、単にファイルを作成しているのか分からない。ここ は、コードサンプルです:xcode:AVFoundationを使用してオーディオを録音した後にオーディオファイルを保存する方法

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

tempRecFile = [NSURL fileURLWithPath:soundFilePath]; 

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

recorder = [[AVAudioRecorder alloc] initWithURL:tempRecFile settings:recSettings error:nil]; 
[recorder setDelegate:self]; 
[recorder prepareToRecord]; 
[recorder record]; 

私はこの問題hereに対する可能な解決策を見ましたが、私はiOSのに新たなんだので、私は本当にそれを取得しないか、それだけで私のために働いていないんです。

コード例を参考にしてください。私はAVFoundationや他のクラスについてのドキュメントを読んだが、肯定的な結果は得られなかった。

SOS :)

UPDATE これは私の記録方法である:

-(IBAction)recording{ 
    //***** METHOD for recording 

    if(isNotRecording){ 
     isNotRecording = NO; 
     [recButton setTitle:@"Stop" forState:UIControlStateNormal]; 
     recStateLabel.text = @"Recording"; 

     [recorder setDelegate:self]; 
     [recorder prepareToRecord]; 
     [recorder record]; 

    } 
    else{ 
     isNotRecording = YES; 
     [recButton setTitle:@"Rec Begin" forState:UIControlStateNormal]; 
     playButton.hidden = NO; 
     recStateLabel.text = @"Not recording"; 
     [recorder stop]; 
    } 


} 

これは私のviewDidLoadである:

- (void)viewDidLoad 
{ 
    //record sound test code - start 
    isNotRecording = YES; 
    //playButton.hidden = YES; 
    recStateLabel.text = @"Not recording"; 

    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

    [audioSession setActive:YES error:nil]; 
    //record sound test code - END 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    // code for generating the sound file that is to be saved 
    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; 

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

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

    } else { 
     [recorder prepareToRecord]; 
    } 

} 

これは私の再生方法である:

-(IBAction)playback{ 
    //AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil]; 
    NSError *error; 
    // setting the uri of the formed created file 
    NSArray *dirPaths; 
    NSString *docsDir; 



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

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL error:&error]; 



[player setNumberOfLoops:0]; 
[player prepareToPlay]; 
[player setVolume:1]; 
[player play]; 
NSLog(@"URL je: %@", soundFileURL); 


//method 2 
if (error) 
    NSLog(@"Error: %@", 
      [error localizedDescription]); 
else 
{ 

    [player play]; 
    if(player.isPlaying==YES) 
    { 
     NSLog(@"It's playing"); 
    } 
} 

}

これは私が何をするかを明確にしています。 私はどこが間違っているかもしれないと示唆していますか?あなたが永続的にファイルにデータを保存するために録音を停止する必要が

答えて

2

:FYI

[recorder stop]; 
+0

こんにちは@mahboudz、私は[レコーダーの停止]を使用して録音を停止します。私はそれが問題だとは思わない。録音をやめた後、録音した音声を、録音したAVplayerと同じURLで再生することができます。問題は、アプリケーションを再起動してもサウンドが再生されないということです。 Tahtは永久に保存されず、セッション/インスタンス中にのみ保存されることを意味します。 – iPhoneNoob

+0

録音を停止した後にファイルの長さがゼロであると言っていますか? – mahboudz

+0

デリゲートメソッドを実装しましたか? ' - (void)audioRecorderEncodeErrorDidOccur :(AVAudioRecorder *)レコーダーエラー:(NSError *)error'?これは、暗号化の際にエラーがあったかどうかを示します。そしていつあなたは '-stop'を呼びますか?アプリがバックグラウンドに入るのはいつですか?または前に? – mahboudz

1

- 「[レコーダーprepareToRecord]」の方法は、以前記録されたファイルを上書きし、新鮮なオーディオファイルを作成します。

3

問題は、(viewDidLoadメソッドの)XCodeに[recorder prepareToRecord]を指定するときです。以前に録音されたオーディオファイルが取り除かれます。 "if"ステートメントでそのコードを削除すると、終了してアプリに戻ったときにオーディオファイルが保存されます。

関連する問題