gpt4 book ai didi

ios - 只播放一次声音,条件

转载 作者:行者123 更新时间:2023-11-29 01:49:32 25 4
gpt4 key购买 nike

只有当我的 iPhone 有某种倾向时,我才尝试播放声音。我这样做:

- (void)viewDidLoad {
[super viewDidLoad];
self.playing = NO;

deviceQueue = [[NSOperationQueue alloc] init];
motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 5.0 / 60.0;

[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical
toQueue:deviceQueue
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CGFloat x = motion.gravity.x;
CGFloat y = motion.gravity.y;
CGFloat z = motion.gravity.z;

CGFloat angle = atan2(y, x) + M_PI_2; // in radians
CGFloat angleDegrees = angle * 180.0f / M_PI;


CGFloat r = sqrtf(x*x + y*y + z*z);
CGFloat tiltForwardBackward = acosf(z/r) * 180.0f / M_PI - 90.0f;




CMMotionManager* motionManager = [[CMMotionManager alloc] init];
[motionManager startAccelerometerUpdates];
CMAccelerometerData* data = [motionManager accelerometerData];
while ( data.acceleration.x == 0 )
{
data = [motionManager accelerometerData];
}
NSLog(@"x = %f, y = %f, z = %f.", data.acceleration.x, data.acceleration.y, data.acceleration.z);

NSLog(@"angleDegrees = %f, anglee = %f", angleDegrees, angle);
NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"End-clap" ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

NSError *error = nil;
_audioPlayer4 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
_audioPlayer4.delegate =self;

if (angleDegrees<180 && angleDegrees>0) {
self.guessImg.image=[UIImage imageNamed:@"guessit-Recovered.png"];
self.wordLabel.text=@"Justin Bieber";
}
else
{
self.wordLabel.text=@"Place on Forehead";

if ([self.wordLabel.text isEqualToString:@"Place on Forehead"]) {
if(_playing)
{


_audioPlayer4.numberOfLoops=1;
self.wordLabel.text=@"Justin";
[_audioPlayer4 play];
self.playing=YES;}


}


}

}];
}];


}


- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"playing has finished");
self.playing = NO;
}

它正在播放,但只是开始,一秒钟,然后一次又一次地开始,没有完成所有的声音。

我想这是因为 angleDegrees 一直在变化。请知道如何解决这个问题?

最佳答案

您可以设置一个 BOOL 来指示播放器正在播放并使用表示播放器停止的委托(delegate)回调。

@property(atomic) BOOL playing;

一旦满足第一个 if 条件,设置为 YES

根据 Apple 关于属性 playing 的文档:

Do not poll this property to determine when playback has completed, instead implement the specified delegate method.

相反,我们依赖于以下委托(delegate)方法:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"playing has finished");
self.playing = NO;
}

为了获得回调,我们需要将自己设置为delegate:

_audioPlayer4 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
_audioPlayer4.delegate = self;

最后,我们可以用以下内容替换您的内部 if:

if(!playing)
{
self.playing = YES;
NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"End-clap" ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

.......

[_audioPlayer4 play];
}

并且在 viewDidLoad 中,将您的属性设置为 NO,最初:

self.playing = NO;

附录:

确保您的 Controller 也采用该协议(protocol)。

即,

@interface yourViewController : UIViewController <AVAudioPlayerDelegate>

关于ios - 只播放一次声音,条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31558006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com