Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone Can';t使用AVAudioRecorder永久保存音频_Iphone_Objective C_Ios_Avaudioplayer_Avaudiorecorder - Fatal编程技术网

Iphone Can';t使用AVAudioRecorder永久保存音频

Iphone Can';t使用AVAudioRecorder永久保存音频,iphone,objective-c,ios,avaudioplayer,avaudiorecorder,Iphone,Objective C,Ios,Avaudioplayer,Avaudiorecorder,我正在制作的一个应用程序有几个视图,每个视图都有一个记录、停止和播放按钮。其想法是用户可以为每个视图录制不同的声音文件 我可以在每个视图上录制和播放声音,但当我离开视图,然后再向后导航时,声音就消失了 我很抱歉在下面包含这么多代码,但这是我需要深入了解的事情 代理.h #import <UIKit/UIKit.h> #import <AudioToolbox/AudioServices.h> #import <AVFoundation/AVFoundation.h&

我正在制作的一个应用程序有几个视图,每个视图都有一个记录、停止和播放按钮。其想法是用户可以为每个视图录制不同的声音文件

我可以在每个视图上录制和播放声音,但当我离开视图,然后再向后导航时,声音就消失了

我很抱歉在下面包含这么多代码,但这是我需要深入了解的事情

代理.h

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

@interface humptyDumptyAppDelegate : UIResponder <UIApplicationDelegate>

{
    NSArray *dirPaths;
    NSString *docsDir;
    NSString *soundFilePathPage1;                               
    NSString *soundFilePathPage2;
    NSString *soundFilePathPage3;
    NSString *soundFilePathPage4;
    NSString *soundFilePathPage5;
    NSString *soundFilePathPage6;

}

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

//example getter and setter functions
- (NSArray*) getDirPaths;
- (void) setDirPaths:(NSArray*)myDirPath;
- (NSString*) getDocsDir;
- (NSString*) soundFilePathForPageNumber:(int)pageNumber;

@end
page1.m

#import "humptyDumptyAppDelegate.h"

@implementation humptyDumptyAppDelegate

@synthesize window = _window;
@synthesize audioPlayer;
@synthesize audioRecorder;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];

    soundFilePathPage1 = [docsDir
                          stringByAppendingPathComponent:@"audiopage1.caf"];
    soundFilePathPage2 = [docsDir
                          stringByAppendingPathComponent:@"page2.caf"];
    soundFilePathPage3 = [docsDir
                          stringByAppendingPathComponent:@"page3.caf"];
    soundFilePathPage4 = [docsDir
                          stringByAppendingPathComponent:@"page4.caf"];
    soundFilePathPage5 = [docsDir
                          stringByAppendingPathComponent:@"page5.caf"];
    soundFilePathPage6 = [docsDir
                          stringByAppendingPathComponent:@"page6.caf"];
    return YES;

}
//getter function
- (NSArray*) getDirPaths{    
    return dirPaths;
}
//setter function
- (void) setDirPaths:(NSArray*)myDirPath{
    dirPaths = myDirPath;
}
// get docs directory
-(NSString*) getDocsDir{
    return docsDir;
}
// get sound file for page, passing the page number as an argument
-(NSString*) soundFilePathForPageNumber:(int)pageNumber{
    switch (pageNumber) {
        case 1:
            return soundFilePathPage1;
            break;
        case 2:
            return soundFilePathPage2;
            break;
        case 3:
            return soundFilePathPage3;
            break;
        case 4:
            return soundFilePathPage4;
            break;
        case 5:
            return soundFilePathPage5;
            break;
        case 6:
            return soundFilePathPage6;
            break;
    }
    return nil;
}
//this is called in viewDidLoad
-(void) prepareForAudioRecording
{
    btnPlay.enabled = NO;
    btnStop.enabled = NO;

    int page = 1;
    NSString *audioFilePath = [appDelegate soundFilePathForPageNumber:page]; 
    NSURL *soundFileURL = [NSURL fileURLWithPath:audioFilePath];
    NSError *error;
    NSDictionary *recordSettings = [NSDictionary 
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16], 
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2], 
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0], 
                                    AVSampleRateKey,
                                    nil];
    appDelegate.audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);

    } else {
        [appDelegate.audioRecorder prepareToRecord];
    }
}

- (IBAction)recordAudio:(id)sender {
    if (!appDelegate.audioRecorder.recording)
    {
        btnPlay.enabled = NO;
        btnStop.enabled = YES;
        [appDelegate.audioRecorder record];

    }
}

- (IBAction)stopAudio:(id)sender {
    btnStop.enabled = NO;
    btnPlay.enabled = YES;
    btnRecord.enabled = YES;


    if (appDelegate.audioRecorder.recording)
    {
        [appDelegate.audioRecorder stop];
        [self audioRecorderDidFinishRecording:appDelegate.audioRecorder successfully:YES];
    } else if (appDelegate.audioPlayer.playing) {
        [appDelegate.audioPlayer stop];

    }
}

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
    if (flag == YES){
        NSLog(@"finished recording");
        [appDelegate.audioPlayer.data writeToFile:[appDelegate soundFilePathForPageNumber:1] atomically:YES];
    }
}
就像我说的,我很抱歉包含这么多代码,但我不确定问题出在哪里。我正在调用
audiorecorderdifinishrecording:
方法中的
writeToFile
方法。我不知道这是否正确,但我感觉这不是问题的根源


请帮忙

此代码保存到音频文件中

把文件拷贝到文档目录怎么样

BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@" sound.caf"];
    success = [fileManager fileExistsAtPath:writableDBPath];

    if (!success){
        // The writable database does not exist, so copy the default to the appropriate location.
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sound.caf"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

        NSError *attributesError;
        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:writableDBPath error:&attributesError];

        NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
        long long fileSize = [fileSizeNumber longLongValue];
        NSLog(@"file size: %lld",fileSize);

        if (!success) {
            NSLog(@"Failed to create writable database file with message: %@", [error localizedDescription]);
        }

    }

原来问题是,在我的一个页面中,我的
prepareForAudioRecording
出现在
viewdide
中,这会自动覆盖保存的音频。将其移动到
viewdiload
就我所知,这就是我所得到的,但我发现了问题所在:在我的一个页面中,我在
viewdilead
中进行了
prepareForAudioRecording
的操作,它自动覆盖了保存的音频。谢谢。很高兴知道,你应该在这个问题上加上你自己的答案,并把它标记为suchOh好的,确保你这样做!20小时还是20年?我们现在是2015年!(这一答案将在17年后进行核对)