只播放一次声音,条件

Play sound only one time, condition

我试图仅在 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 来指示玩家正在玩,并使用表示玩家停止的委托回调。

@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.

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

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