Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.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
Ios SIGABRT:-[NSConcreteFileHandle seektoEndofile]:没有这样的文件或目录_Ios_Objective C_Sigabrt_Nsfilehandle - Fatal编程技术网

Ios SIGABRT:-[NSConcreteFileHandle seektoEndofile]:没有这样的文件或目录

Ios SIGABRT:-[NSConcreteFileHandle seektoEndofile]:没有这样的文件或目录,ios,objective-c,sigabrt,nsfilehandle,Ios,Objective C,Sigabrt,Nsfilehandle,因此,我将加速度计值写入csv文件。有时有效,有时无效。通常情况下,当加速计打开超过5秒时,它会崩溃,我会得到错误信息 代码如下: int recordbuttonstatus=0; int playbuttonstatus=0; int count; NSString *dataStr; NSString *dirName; NSFileManager *filemgr; NSString *audiofilename = @"Pneumonia_audio.wav"; NSString

因此,我将加速度计值写入csv文件。有时有效,有时无效。通常情况下,当加速计打开超过5秒时,它会崩溃,我会得到错误信息

代码如下:

int recordbuttonstatus=0;
int playbuttonstatus=0;


int count;

NSString *dataStr;
NSString *dirName;
NSFileManager *filemgr;
NSString *audiofilename = @"Pneumonia_audio.wav";
NSString *audiofilepath;
NSString *csvfileName = @"accel.csv";
NSFileHandle *myHandle;


- (void)viewDidLoad {
    [super viewDidLoad];
    playbuttonimage.enabled=FALSE;
    NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               audiofilename,
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];



    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    // Define the recorder setting
    NSDictionary *recordSetting = [NSDictionary
                                   dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:AVAudioQualityMin],
                                   AVEncoderAudioQualityKey,
                                   [NSNumber numberWithInt:16],
                                   AVEncoderBitRateKey,
                                   [NSNumber numberWithInt: 1],
                                   AVNumberOfChannelsKey,
                                   [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                   [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                                   nil];

    // Initiate and prepare the recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];

}

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

- (IBAction)recordbuttonaction:(id)sender {
    if (recordbuttonstatus==0){
    [recordbuttonimage setImage:[UIImage imageNamed:@"stoprecord.png"] forState:UIControlStateNormal];
        recordbuttonstatus=1;
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];

        // Start recording
        [recorder record];

        //Prepare writing to csv
        dirName = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        filePath = [dirName stringByAppendingPathComponent: csvfileName];
        filemgr = [NSFileManager defaultManager];
        if ([filemgr fileExistsAtPath:filePath]) {
            NSLog(@"File %@ being overwritten...", csvfileName);
            NSError *error = nil;
            [filemgr removeItemAtPath:filePath error:&error];}

        else {
            NSLog(@"File %@ doesn't exist.  Making %@.", csvfileName,csvfileName);
        }
        [[NSFileManager defaultManager] createFileAtPath:filePath
                                                contents:nil
                                              attributes:nil];
        myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

        //Start accelerometer
        count =0;
        motionManager = [[CMMotionManager alloc] init];
        motionManager.deviceMotionUpdateInterval=.02;

        [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
            CMAcceleration userAcceleration = deviceMotion.userAcceleration;
            float accelval = userAcceleration.y*9.81;
            count = count + 1;
            NSString *accelvalue = [[NSString alloc] initWithFormat:@"%f\n",accelval];
            NSLog(@"number: %d Accelval: %f",count, accelval);
            [myHandle seekToEndOfFile];
            [myHandle writeData:[accelvalue dataUsingEncoding:NSUTF8StringEncoding]];

        }];
    }
        else{
            [motionManager stopDeviceMotionUpdates];
            [myHandle closeFile];
            [recordbuttonimage setImage:[UIImage imageNamed:@"record.png"] forState:UIControlStateNormal];
        recordbuttonstatus=0;
        playbuttonimage.enabled=TRUE;
        [playbuttonimage setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
        [recorder stop];
        }

}
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
        CMAcceleration userAcceleration = deviceMotion.userAcceleration;
        float accelval = userAcceleration.y*9.81;
        count = count + 1;
        NSString *accelvalue = [[NSString alloc] initWithFormat:@"%f\n",accelval];
        NSLog(@"number: %d Accelval: %f",count, accelval);
        NSFileHandle *myHandle;
        myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
        [myHandle seekToEndOfFile];
        [myHandle writeData:[accelvalue dataUsingEncoding:NSUTF8StringEncoding]];
        [myHandle closeFile];
    }];

因此,我在调用[fileHandle closeFile]时尝试了启动和更改,我得到了:

我需要确保录制后文件句柄不会更改,或者closeFile调用得太早/太迟。所以我把所有的fileHandle行放在块中,加速计将为每个值更新。它担心它会延迟我的代码,但它没有延迟。
代码如下:

int recordbuttonstatus=0;
int playbuttonstatus=0;


int count;

NSString *dataStr;
NSString *dirName;
NSFileManager *filemgr;
NSString *audiofilename = @"Pneumonia_audio.wav";
NSString *audiofilepath;
NSString *csvfileName = @"accel.csv";
NSFileHandle *myHandle;


- (void)viewDidLoad {
    [super viewDidLoad];
    playbuttonimage.enabled=FALSE;
    NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               audiofilename,
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];



    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    // Define the recorder setting
    NSDictionary *recordSetting = [NSDictionary
                                   dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:AVAudioQualityMin],
                                   AVEncoderAudioQualityKey,
                                   [NSNumber numberWithInt:16],
                                   AVEncoderBitRateKey,
                                   [NSNumber numberWithInt: 1],
                                   AVNumberOfChannelsKey,
                                   [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                   [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                                   nil];

    // Initiate and prepare the recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];

}

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

- (IBAction)recordbuttonaction:(id)sender {
    if (recordbuttonstatus==0){
    [recordbuttonimage setImage:[UIImage imageNamed:@"stoprecord.png"] forState:UIControlStateNormal];
        recordbuttonstatus=1;
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];

        // Start recording
        [recorder record];

        //Prepare writing to csv
        dirName = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        filePath = [dirName stringByAppendingPathComponent: csvfileName];
        filemgr = [NSFileManager defaultManager];
        if ([filemgr fileExistsAtPath:filePath]) {
            NSLog(@"File %@ being overwritten...", csvfileName);
            NSError *error = nil;
            [filemgr removeItemAtPath:filePath error:&error];}

        else {
            NSLog(@"File %@ doesn't exist.  Making %@.", csvfileName,csvfileName);
        }
        [[NSFileManager defaultManager] createFileAtPath:filePath
                                                contents:nil
                                              attributes:nil];
        myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

        //Start accelerometer
        count =0;
        motionManager = [[CMMotionManager alloc] init];
        motionManager.deviceMotionUpdateInterval=.02;

        [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
            CMAcceleration userAcceleration = deviceMotion.userAcceleration;
            float accelval = userAcceleration.y*9.81;
            count = count + 1;
            NSString *accelvalue = [[NSString alloc] initWithFormat:@"%f\n",accelval];
            NSLog(@"number: %d Accelval: %f",count, accelval);
            [myHandle seekToEndOfFile];
            [myHandle writeData:[accelvalue dataUsingEncoding:NSUTF8StringEncoding]];

        }];
    }
        else{
            [motionManager stopDeviceMotionUpdates];
            [myHandle closeFile];
            [recordbuttonimage setImage:[UIImage imageNamed:@"record.png"] forState:UIControlStateNormal];
        recordbuttonstatus=0;
        playbuttonimage.enabled=TRUE;
        [playbuttonimage setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
        [recorder stop];
        }

}
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
        CMAcceleration userAcceleration = deviceMotion.userAcceleration;
        float accelval = userAcceleration.y*9.81;
        count = count + 1;
        NSString *accelvalue = [[NSString alloc] initWithFormat:@"%f\n",accelval];
        NSLog(@"number: %d Accelval: %f",count, accelval);
        NSFileHandle *myHandle;
        myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
        [myHandle seekToEndOfFile];
        [myHandle writeData:[accelvalue dataUsingEncoding:NSUTF8StringEncoding]];
        [myHandle closeFile];
    }];

这些代码乍一看并不完整?我想知道这是否是一个随机的硬件问题,因为我让它工作到532个值,然后当我重新运行应用程序时,它崩溃了,当它超过500。直到你按下按钮停止录音,错误才会显示。是的,这是一段摘录。如果合适的话,我会发布整个代码。你应该重构代码,不使用每个新值打开/写入/关闭文件。那真是效率低下。