简体   繁体   中英

How To save Recorded audio to my app?

Please help me i tired to getting answer using following Answer in stackOverflow

Ans 1

Ans 2

My code is

- (IBAction)RecordBtnClicked:(UIButton *)sender {
if (sender.isSelected) {
    if (!recorder.recording) {
 [session setActive:YES error:nil];
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        temporaryRecFile = [prefs URLForKey:@"Test1"];
        player = [[AVAudioPlayer alloc]initWithContentsOfURL:temporaryRecFile error:nil];
        player.delegate = self;
        player.volume = 1;
        [player prepareToPlay];
        [player play];
        [sender setSelected:false];
    }
    `[recordBtn setImage:[UIImage imageNamed:@"a_recordp.png"] forState:UIControlStateSelected];
    [recorder stop];
}`else{
    if (player.playing) {
        [player stop];
    }`
    if (!recorder.recording) {
        [recordBtn setImage:[UIImage imageNamed:@"a_records.png"] forState:UIControlStateSelected];
        NSString *documentPath = [pathComponent objectAtIndex:0];
        NSString *pathTosave =[documentPath stringByAppendingPathComponent:[self fileNameForRecording]];
        NSURL *url = [NSURL fileURLWithPath:pathTosave];
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setURL:url forKey:@"test1"];
        session = [AVAudioSession sharedInstance];
        [session setActive:YES error:Nil];
       // temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByDeletingLastPathComponent]];
        [recorder record];
        recordTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimeInterval) userInfo:nil repeats:YES];
    }
    [sender setSelected:true];
    [discardBtn setHidden:false];
    [saveBtn setHidden:false];
}

}

I have One button On first tap Recording is start and on 2 tap same button recording is stop and also third tap on same button i want to play same sound but i don't get desired file.

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

it gives nil value ? please provide me some suggestions for this if i do code in wrong way ? any answer will be appreciated Thanks in advance

Sometimes you cannot rely on iOS to save your data, you need to force it to do so by synchronizing.

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setURL:url forKey:@"test1"];
[prefs synchronize]; //this will ensure it is written to the disk

Also one advice I can give you is to split that button functionality, you are doing way too much (record, stop record and play) in this IBAction. Perhaps it will be better for you to have 2 buttons first one recordOrStop() and playOrStop() but that's just my point of view as it will avoid problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM