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

时间:2017-03-26 16:37:10

标签: 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 *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];
        }

}

1 个答案:

答案 0 :(得分:0)

所以当我调用[fileHandle closeFile]时,我尝试了启动和更改,我得到了它:

我需要确保fileHandle在录制后没有机会改变,或者closeFile被调用得太早/太晚。因此,我将所有fileHandle行放在加速度计将为每个值更新的块中。它担心它会落后于我的代码,但事实并非如此 这是代码:

[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];
    }];