Best way for looping and using background sounds i

2019-09-01 16:34发布

问题:

Lets say I am dragging my finger on screen and I have 1 second .caf sound file in my bundle.

So what is the best way to play my sound file in a loop till i am dragging my fingers. And it should stop whenever I remove touches.

I know touches implementation. Just post your views about using sound file.

回答1:

See AVAudioPlayer class, it worked pretty well for me for similar behaviour described in your question.



回答2:

This is how I did it. At the moment, I don't know of a more efficient method...but here's what I used to loop my background sound. Hope this helps.

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/yourAudio.caf", [[NSBundle mainBundle] resourcePath]]];

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];