开发者

play sound while sliding finger on iphone

i like to add the function playing sounds when either sliding fingers on the surface to my iphone app. the sounds are playing now only when pushing buttons. i suppose this works with TouchesMoved, but i don't know how to implement the "AVAudioPlayer" code to开发者_StackOverflow中文版 the touches event code. please any code sample, thanks!


As far as I'm aware, TouchesMoved is used to trigger events on the screen (when 'moved over') which in turn can trigger sound events. I don't beleive there is a way you can produce a sound when the user is just 'sliding' around the screen and not interacting with any objects.

This is something I've looked into myself but didn't come up with a solution, but I'll hopefully be corrected in this thread if wrong!


You can instantiate an AVAudioPlayer inside of the touchesMoved:withEvent: method and play a sound. You need to keep a strong reference to each AVAudioPlayer you create or else you will not hear a thing.

To keep track of multiple AVAudioPlayers store them in an array, and remove them from the array when they are finished playing (which you can find out using the audioPlayerDidFinishPlaying:successfully: delegate method).

Keep in mind that touchesMoved:withEvent: may be triggered in a super high tempo, so your array may grow too large. It's wise to only play a sound when something acutally happens on the screen, such as the next piano key being touched, or to keep a counter and only play a sound every n touches.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜