所以我认为我在主线程上有startDeviceMotionUpdatesToQueue,但显然情况并非如此,因为我应该在NSLogs之后获得NSLog更新。
2017-03-26 14:01:40.422661 Pneumonia Diagnosing App[950:200771] File
accel.wav being overwritten...
...
2017-03-26 14:01:40.617392 Pneumonia Diagnosing App[950:200771] j=229320, finalfilelength = 229320, and i=114704, and filelength = 114660
2017-03-26 14:01:40.622864 Pneumonia Diagnosing App[950:200771] number: 66 Accelval: 0.004889
2017-03-26 14:01:40.623466 Pneumonia Diagnosing App[950:200771] number: 67 Accelval: 0.005058
我设置了startDeviceMotionUpdatesToQueue,如下所示:
[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];
}];
我相信mainQueue是主线程。
在我的代码中,我希望将加速度计值记录到csv中,然后经过大量处理后,我希望将它们放入wav文件中。
在startDeviceMotionUpdatesToQueue块之后(通过点击记录按钮发生),点击停止按钮并发生这种情况:
[motionManager stopDeviceMotionUpdates];
[recordbuttonimage setImage:[UIImage imageNamed:@"record.png"] forState:UIControlStateNormal];
recordbuttonstatus=0;
playbuttonimage.enabled=TRUE;
[playbuttonimage setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[recorder stop];
// Process CSV File on new Thread
[self prepareaccelvalsforwav];
// Combine data channels
[self combinechannels];
记录应该出现的行的代码发生在combine渠道方法中,我在下面分享了一些代码。
创建wav后,我有日志告诉我wav中字节的索引和文件的音频长度:
FILE *finaldata = fopen([finalaudiofilepath UTF8String], "ab");
i=44;
k=4096;
j=0;
for (j = 0; j < (mixedaudiobufferlength);) {
buffer3[0] = audiobytedatabuffer[k];
buffer3[1] = audiobytedatabuffer[k + 1];
buffer3[2] = accelbytedatabuffer[i];
buffer3[3] = accelbytedatabuffer[i + 1];
j = j + 4;
i = i + 2;
k = k + 2;
fwrite(buffer3,1,4,finaldata);
fflush(finaldata);
}
NSLog(@"j=%d, finalfilelength = %d, and i=%d, and filelength = %llu",j,mixedaudiobufferlength,i,accelfileSize);
fclose(finaldata);
您可以在我发布的日志中看到。然而,加速度计NSLog再次弹出。我该怎么做才能防止这种情况,让加速度计在写入wav文件之前停止记录值?