嗨,在我的项目中,我需要在通话激活时调用加速度计,我使用此代码在通话期间调用加速度计。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if(isSlamOff == FALSE){
NSLog(@"Slame is off");
if(callState == CTCallStateConnected) {
UIAccelerometer *accelerometer =[UIAccelerometer sharedAccelerometer];
accelerometer.updateInterval = 0.1;
accelerometer.delegate = self;
}
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
UIAccelerationValue x, y, z;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
NSLog(@"x is %4.2f, y is %4.2f and z is %4.2f",x,y,z);
magnitude = sqrt(acceleration.x * acceleration.x
+ acceleration.y * acceleration.y
+ acceleration.z * acceleration.z);
NSLog(@"%f",magnitude);
if(magnitude > 2.5){
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"slamshort" ofType:@"wav"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error: nil];
[newPlayer prepareToPlay];
NSLog(@"Music Is Playing");
AudioSessionSetProperty(kAudioSessionOverrideAudioRoute_Speaker, sizeof(newPlayer),&newPlayer);
[newPlayer setVolume: 1.0];
[newPlayer play];
}
}
但它既不显示通话状态,也不在通话过程中在后台运行加速,我从苹果iOS最新的多任务功能指南中使用了这个,但不显示通话状态,加速计不在后台运行。是否可以在通话过程中感知加速并在通话过程中播放音频文件,或者在 iOS 中是不可能的。或者,如果有可能,那么使之成为可能的方法或代码是什么。