簡體   English   中英

AVAudioRecorder在特殊情況下將不會錄制

[英]AVAudioRecorder will not record in special cases

我正在嘗試使用AVAudioRecorder記錄用戶語音。當我使用此代碼時,它可以工作:

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat: @"yyyy-MM-dd ss"];
NSString *dateWithString = [formatter stringFromDate:date];
NSLog(@"%@",dateWithString);
NSString *soundFilePath1 = [docsDir
                           stringByAppendingPathComponent:nil];
NSString *soundFilePath = [soundFilePath1
                           stringByAppendingPathComponent:@"sound.caf"];

NSLog(@"%@",soundFilePath);

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 *error2;

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

但是,當我使用時它不起作用:

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat: @"yyyy-MM-dd ss"];
NSString *dateWithString = [formatter stringFromDate:date];
NSLog(@"%@",dateWithString);
NSString *soundFilePath1 = [docsDir
                           stringByAppendingPathComponent:dateWithString];
NSString *soundFilePath = [soundFilePath1
                           stringByAppendingPathComponent:@"sound.caf"];

NSLog(@"%@",soundFilePath);

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 *error2;



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

這兩個代碼塊之間的區別是:

NSString *soundFilePath1 = [docsDir stringByAppendingPathComponent:dateWithString];

為什么會這樣呢?

使用此代碼來檢查您的目錄是否正確創建。

NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat: @"yyyy-MM-dd ss"];
    NSString *dateWithString = [formatter stringFromDate:date];
    NSLog(@"%@",dateWithString);
    docsDir = [[dirPaths objectAtIndex:0] stringByAppendingPathComponent:dateWithString];

    NSError *e;
    if (![[NSFileManager defaultManager] fileExistsAtPath:docsDir])    //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:docsDir
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&e])
        {
            NSLog(@"Create directory error: %@", e);
        }
    }

在那之后,

NSString *soundFilePath = [docsDir
                               stringByAppendingPathComponent:@"sound.caf"];

    NSLog(@"%@",soundFilePath);



    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath isDirectory:NO];

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

    NSError *error2;

嘗試這個

-(IBAction)record:(id)sender
{
NSArray *dirPaths;
NSString *docsDir;

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

NSDate  *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat: @"yyyy-MM-dd ss"];
NSString *dateWithString = [formatter stringFromDate:date];
NSLog(@"%@",dateWithString);

NSString *dateFolderPath = [docsDir stringByAppendingPathComponent:dateWithString];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dateFolderPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dateFolderPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

NSString *soundFilePath = [dateFolderPath
                           stringByAppendingPathComponent:@"sound.caf"];

NSLog(@"%@",soundFilePath);

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 *error2;

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

-(IBAction)stop:(id)sender
{
  [recorder stop];
  NSLog(@"stop");
}

AVAudioRecorder會在文檔目錄或臨時目錄的頂層記錄音頻。 如果嘗試在文檔目錄中創建的文件夾中記錄音頻,則它將失敗。 始終將音頻記錄在臨時目錄中,並將移動文件記錄到文檔目錄中的文件夾之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM