5

I have made 2 iPhone apps which can record audio and save it to a file and play it back again.

One of them uses AVAudiorecorder and AVAudioplayer. The second one is Apple's SpeakHere example with Audio Queues.

Both run on Simulater as well as the Device.

BUT when I restart either app the recorded file is not found!!! I've tried all possible suggestions found on stackoverflow but it still doesnt work!

This is what I use to save the file:

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound1.caf"]; 
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
4

1 に答える 1

16

やっと解決しました。問題は、AVAudioRecorderを設定し、ViewController.mのviewLoadにパスをファイルして、同じ名前の既存のファイルを上書きしていたことです。

  1. オーディオを録音してファイルに保存し、アプリを停止した後、Finderでファイルを見つけることができました。(/Users/xxxxx/Library/Application Support / iPhone Simulator /6.0/Applications/0F107E80-27E3-4F7C-AB07-9465B575EDAB/Documents/sound1.caf)
  2. アプリケーションを再起動すると、レコーダーのセットアップコード(viewLoadから)は、次の古いファイルを上書きするだけでした。

    sound1.caf

  3. 新しいもので。同じ名前ですが、内容はありません。

  4. 再生は、空の新しいファイルを再生するだけです。->明らかに音が出ない。


これが私がしたことです:

NSUserdefaultsを使用して、後でplayBackメソッドで取得できるように記録されたファイル名のパスを保存しました。


ViewController.mのviewLoadをクリーンアップしました:

- (void)viewDidLoad
{

     AVAudioSession *audioSession = [AVAudioSession sharedInstance];

     [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

     [audioSession setActive:YES error:nil];

     [recorder setDelegate:self];

     [super viewDidLoad];
}

ViewController.mで編集されたレコード:

- (IBAction) record
{

    NSError *error;

    // Recording settings
    NSMutableDictionary *settings = [NSMutableDictionary dictionary];

    [settings setValue: [NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    [settings setValue: [NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey];
    [settings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
    [settings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
    [settings setValue:  [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey];

    NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath_ = [searchPaths objectAtIndex: 0];

    NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[self dateString]];

    // File URL
    NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH];


    //Save recording path to preferences
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


    [prefs setURL:url forKey:@"Test1"];
    [prefs synchronize];


    // Create recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

    [recorder prepareToRecord];

    [recorder record];
}

ViewController.mで編集された再生:

-(IBAction)playBack
{

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

[audioSession setActive:YES error:nil];


//Load recording path from preferences
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


temporaryRecFile = [prefs URLForKey:@"Test1"];



player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];



player.delegate = self;


[player setNumberOfLoops:0];
player.volume = 1;


[player prepareToPlay];

[player play];


}

そして、ViewController.mに新しいdateStringメソッドを追加しました。

- (NSString *) dateString
{
    // return a formatted string for a file name
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"ddMMMYY_hhmmssa";
    return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".aif"];
}

これで、NSUserdefaultsを介して最後に記録されたファイルをロードできます。

    //Load recording path from preferences
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


temporaryRecFile = [prefs URLForKey:@"Test1"];



player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];

(IBAction)playBackで。temporaryRecFileは、ViewControllerクラスのNSURL変数です。

次のViewController.hとして宣言されています:

@interface SoundRecViewController : UIViewController <AVAudioSessionDelegate,AVAudioRecorderDelegate, AVAudioPlayerDelegate>
{
......
......
    NSURL *temporaryRecFile;

    AVAudioRecorder *recorder;
    AVAudioPlayer *player;

}
......
......
@end
于 2013-02-25T23:49:33.813 に答える