2016-08-23 5 views
2

AVAudioFileNSDictionaryで文書ディレクトリに保存します。誰でも助けてくれますか?AVAudioFileをドキュメントディレクトリに保存する方法は?

AVAudioFile *audiofile=[[AVAudioFile alloc] initForWriting:destinationURL settings:settings error:&error]; 

...ディレクトリを文書化するドキュメントディレクトリへ

+0

このリンクをしてくださいhttp://stackoverflow.com/questions/15059089/record-audio-and-save -permanently-in-ios –

+0

どのように保存する?これは問題です。他にも、このオーディオファイルを保存するためにどこに追加すればいいですか? –

+0

おかげで兄弟ですが、私の "audiofile"を追加する場所はありません。 –

答えて

0

パスこのオーディオファイルを保存します。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; 

は、ドキュメントディレクトリにオーディオファイルを保存:

BOOL status = [NSDictionary writeToFile:filePath atomically:YES]; 
if(status){ 
NSLog(@"File write successfully"); 
} 
0
- (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"]; 
} 

以下のように保存しますドキュメント

23Aug16_044104PM.aif

に我々は好きで上記の保存なぜ我々は今、混乱することはできませんtime.Soすることにより、以前の1次1をdifferenciateすることができます。

ViewController.h

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController : UIViewController<AVAudioSessionDelegate,AVAudioRecorderDelegate, AVAudioPlayerDelegate> 
{ 

    NSURL *temporaryRecFile; 
    AVAudioRecorder *recorder; 
    AVAudioPlayer *player; 
} 

- (IBAction)actionRecordAudion:(id)sender; 

- (IBAction)actionPlayAudio:(id)sender; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 
[audioSession setActive:YES error:nil]; 
[recorder setDelegate:self]; 
} 

- (IBAction)actionRecordAudion:(id)sender 
{ 

    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]]; 
    NSLog(@"the path is %@",pathToSave); 

    // 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]; 
} 
- (IBAction)actionPlayAudio:(id)sender 
{ 
    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]; 
} 

Record and Save Audio Permanently

Record Audio File and save Locally

はちょうど今、私はiPhone 4Sと以下のコードを試してみました、それが完璧に動作します。

AudioViewController.h

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
@interface AudioViewController : UIViewController<AVAudioSessionDelegate,AVAudioRecorderDelegate,AVAudioPlayerDelegate> 

- (IBAction)actionRecordAudio:(id)sender; 
- (IBAction)actionPlayAudio:(id)sender; 
- (IBAction)actionStopAudio:(id)sender; 

@property (strong, nonatomic) AVAudioRecorder *audioRecorder; 
@property (strong, nonatomic) AVAudioPlayer *audioPlayer; 

@end 

AudioViewController.m

#import "AudioViewController.h" 

@interface AudioViewController() 

@end 

@implementation AudioViewController 

@synthesize audioPlayer,audioRecorder; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    NSArray *dirPaths; 
    NSString *docsDir; 

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = dirPaths[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; 

    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

    audioRecorder = [[AVAudioRecorder alloc]initWithURL:soundFileURL settings:recordSettings error:&error]; 
    if (error) 
    { 
    NSLog(@"error: %@", [error localizedDescription]); 
    } 
    else { 
    [audioRecorder prepareToRecord]; 
    } 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (IBAction)actionRecordAudio:(id)sender 
{ 
    if (!audioRecorder.recording) 
    { 
    [audioRecorder record]; 
    } 
} 

- (IBAction)actionPlayAudio:(id)sender 
{ 
    if (audioRecorder.recording) 
    { 
     NSError *error; 
     audioPlayer = [[AVAudioPlayer alloc] 
        initWithContentsOfURL:audioRecorder.url 
        error:&error]; 

     audioPlayer.delegate = self; 

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

- (IBAction)actionStopAudio:(id)sender 
{ 
    if (audioRecorder.recording) 
    { 
     [audioRecorder stop]; 
    } 
    else if (audioPlayer.playing) { 
    [audioPlayer stop]; 
    } 
} 

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 

} 

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error 
{ 
    NSLog(@"Decode Error occurred"); 
} 

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag 
{ 
} 

-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error 
{ 
    NSLog(@"Encode Error occurred"); 
} 
@end 

Here is source

+0

いいえ、働いていません........私のために私のAVAudioFile –

+0

私は提供されたコードをあなたが提供するコードは、AVAudioFileを保存するためのソリューションを提供することができます私は録音されたオーディオを再生するためです...私は録音されたオーディオのピッチを変更し、 :( –

+0

私の答えを今すぐチェックしてください。私は私の答えを更新しました – user3182143

関連する問題