したがって、加速度計の値をcsvファイルに書き出しています。ときどき動作し、時には動作しないことがあります。通常、加速度計が5秒以上オンになると、クラッシュし、エラーが発生します。ここでSIGABRT: - [NSConcreteFileHandle seekToEndOfFile]:そのようなファイルまたはディレクトリはありません
はコードです:
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];
}
}
:
ここ は、コードのですか? – bbum
私はそれが532値まで動作するようにしたので、それがランダムなハードウェアの問題かどうか疑問に思っています。そして、アプリケーションを再実行したとき、500を超えるとクラッシュしました。エラーは、ボタンを押して停止するまで表示されません録音。 – Zen
ええ、それは抜粋です。 – Zen