Я пытаюсь воспроизвести звук только тогда, когда у моего 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;
}
и это играет, но только начало, одну секунду, а затем начать снова и снова, не докончив весь звук.
Я предполагаю, что это из-за угла, который постоянно меняется. Любая идея, как это решить, пожалуйста?
[AVAudioPlayer playing]
не лжет. Добавьте запись в этой области. - person trojanfoe   schedule 22.07.2015