只播放一次声音,条件

时间:2015-07-22 08:36:05

标签: ios objective-c avaudioplayer

我只是在我的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一直在变化。请问你能解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

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

@property(atomic) BOOL playing;

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

根据Apple关于财产playing的文件:

  

不要轮询此属性以确定播放何时完成,   而是实现指定的委托方法。

相反,我们依赖以下委托方法:

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

附录:

确保您的控制器也采用该协议。

即,

@interface yourViewController : UIViewController <AVAudioPlayerDelegate>

答案 1 :(得分:0)

因为angleDegrees一直在变化。所以你应该设置一个状态表示声音正在播放。如果正在播放,则在完成之前不能再播放。