开发者

how to play audio file when imageview is at some position on screen which is moving by accelerometer

开发者 https://www.devze.com 2023-01-30 18:44 出处:网络
In my project i am trying to play when my image is moving from one side to another side of half of horizontal screen .

In my project i am trying to play when my image is moving from one side to another side of half of horizontal screen .

i am trying like this

sample code

- (void)accelerometer:(UIAccelerometer *)acel
        didAccelerate:(UIAcceleration *)acceleration {
if (imageView.center.x + translation.x > 160 - ballRadius) {

        NSString *path = [[NSBundle mainBundle] pathForResource:@"warning" ofType:@"m4r"];
        NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

        AVAudioPlayer *p =[[AVAudioPlayer alloc]
                           initWithContentsOfURL:file error:nil];
        [file release];

        self开发者_开发技巧.player = p;
        [p release];

        [player prepareToPlay];
        [player setDelegate:self];
        [self.player play];

    } 
}

but my app is crashing when i am trying like this

can any one please help me

regards


Where is your app crashing? Did you use the Debugger to determine where?

How many times per second is this accelerometer being called? If you are trying to start the same sound that many times, it may be too much for the AVPlayer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号