0

私が取り組んでいるプログラムでは、将来の使用のために大量の AVAudioRecorder ファイルを保存する必要があります。ただし、静的な量のファイルを保存して録音する方法は認識していますが、録音が行われるたびに同じサウンドファイル URL (「sound.caf」など) を使用せずに動的に保存できるようにしたいと考えています。 、必要なときにいつでもそれらのファイルを再生できるようにします。私がこれを行う方法を知っている人はいますか?

以下のコードは、現在の記録コードを示しています。

- (IBAction)playSound:(id)sender {

    if (!self.audioRecorder.recording)
    {
        NSError *error;

        if (self.audioPlayer)
            self.audioPlayer = nil;

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

        self.soundFileURL = [NSURL fileURLWithPath:self.soundFilePath];

        self.audioPlayer = [[AVAudioPlayer alloc] 
                       initWithContentsOfURL:self.soundFileURL                                    
                       error:&error];

        self.audioPlayer.delegate = self;
        [self.audioPlayer prepareToPlay];

        if (error)
            NSLog(@"Error: %@", 
                  [error localizedDescription]);
        else
            [self.audioPlayer play];
    }

}


- (IBAction)recordSound:(id)sender {

    if(!self.audioRecorder.recording)
    {

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

        self.soundFileURL = [NSURL fileURLWithPath:self.soundFilePath];

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

        NSError *error = nil;

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

        if (error)
        {
            NSLog(@"error: %@", [error localizedDescription]);
        } else {
            [self.audioRecorder prepareToRecord];
        }


        [self.audioRecorder record];
        [self.audioRecorder recordForDuration:(NSTimeInterval) 5];
        [self.record setTitle:@"Stop" forState: UIControlStateNormal];
    }
    else if (self.audioRecorder.recording)
    {
        [self.audioRecorder stop];
        [self.record setTitle:@"Record" forState: UIControlStateNormal];

    } 
    else if (self.audioPlayer.playing) {
        [self.audioPlayer stop];
    }
}
4

1 に答える 1

0

あなたの質問を正しく理解すれば、これにアプローチする方法はたくさんあります。サウンド スニペットを録音し、後で再生するために保存するアプリがあります。私のアプローチを共有します。それを取るか、そのままにしておくことができます。

最初に、NSDocuments ではなく一時ディレクトリ (NSTempraryDirectory) を使用しますが、あなたと同じようにレコーダーを作成します。この一時的な場所にある単一のファイルを使用して記録し、記録が終了してユーザーが記録を保存することを確認した後、[NSMutableData dataWithContentsOfFile:] を使用して一時ファイルのデータをメモリにロードし、選択したパスに書き出します。 [FileManager createFileAtPath:] を使用します。ファイルを再現するためのより効率的な方法があるかもしれませんが、これは私の目的には十分効率的です。

createFileAtPath に渡されるパスには、レコーディング ストレージ フォルダーから構築された文字列と、レコーディングごとに生成された uniqueID 文字列を使用します。[[NSProcessInfo processInfo] globalUniqueString] を使用して一意の ID を作成しますが、記録 ID のセット内で一意であることが確実である限り、ハッシュまたはその他の適切な手法を使用できます。

次に、保存したときと同じようにパスを作成するだけで、ID を持っている任意のレコーディングの AVAudioPlayer を作成できます。リストまたはデータベースで ID を追跡していない場合は、記録ディレクトリを列挙して、ファイル名から記録 ID のリストを作成できます。データベースを使用して、録音時間、長さ、件名などのメタデータとともに ID を保存します。

于 2012-09-11T00:53:26.790 に答える