呼叫激活时是否有可能运行加速度计?

发布于 2024-12-10 03:23:08 字数 1554 浏览 0 评论 0原文

您好,在我的项目中,我需要在激活呼叫时调用加速度计,我使用此代码在通话期间调用加速度计。

- (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 中不可能。或者如果可能的话,那么有什么方法或代码可以实现这一点。

Hi In my project I need to call accelerometer while call is activated I use this code for calling accelerometer during call.

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

But it neither show the call status nor run the acceleration in background during call, and I used this from the latest multitasking function guide from apple iOS but not show the status of call and accelerometer is not running in the background. Is it possible to sense acceleration during call and play a audio file during call or it is not possible in iOS. Or if it is possible then what is the way or code to make it possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-12-17 03:23:08

iOS 不是真正的多任务操作系统,因此目前您无法在应用程序处于后台时运行加速计代码。

iOS is not real multitasking operating system, so currently you cannot run accelerometer code while app is at background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文