From your description, it sounds as if you're trying to write a software synthesizer. The only way that you could use AVAudioPlayer
for something like this would be to compose the entire duration of a note as a single WAV file and then play the whole thing with AVAudioPlayer
.
To create a note sound of arbitrary duration, one that begins playing in response to a user action (like tapping a button) and then continues playing until a second user action (like tapping a "stop" button or lifting the finger off the first button) begins the process of ramping the looped region's volume down to zero (the "release" part), you will need to use AudioQueue
(AVAudioPlayer
can be used to play audio constructed entirely in memory, but the entire playback has to be constructed before play begins, meaning that you cannot change what is being played in response to user actions [other than to stop playback]).
Here's another question/answer that shows simply how to use AudioQueue
. AudioQueue
calls a callback method whenever it needs to load up more data to play - you would have to implement all the code that loops and envelope-wraps the original WAV file data.