0

In my app I'm using AVAudioPlayer objects to hold the mp3 files. I have several occasions where I wish to lower the sound level of the files while they are being played.

I understand some of what I'm supposed to be doing, but I don't understand how I define the duration and how do I command that something will happen only for that duration.

I'm looking for something like this:

AVAudioPlayer * aud1 = [I have a method to get the file];
AVAudioPlayer * aud2 = [I have a method to get the file];
NSTimeInterval * aud1_ti = aud1.duration //I haven't tried this yet - is this the right way to get the mp3 play duration?
[aud1 play];
[aud2 play];
while (aud1_ti)
     aud2.volume = 0.5;
aud2.volume = 1.0

To say it in words - I wish that while aud1 is playing, aud2 volume will be reduced in half; and once the file had reached its end the aud2 volume will get back to its full level. I couldn't understand from the documentation how to do that.

Can anyone assist?

Ohad Regev
  • 5,641
  • 12
  • 60
  • 84

1 Answers1

0

Rather than basing this off of the duration what you probably want to do is have the delegate of your AVAudioPlayer respond to – audioPlayerDidFinishPlaying:successfully: and reset the volume at that time. On a very, very basic level you will have something like this:

- (void)startPlaying {
     self.aud1.delegate = self;
     [self.aud1 play];

     self.aud2.volume = 0.5;
     [self.aud2 play];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL) {
     if (self.aud2) {
          self.aud2.volume = 1.0;
     }
}

Note, the code above is obviously incomplete but should point you in the right direction.

David Brainer
  • 6,223
  • 3
  • 18
  • 16
  • David, thanks a lot! I'm still not totally clear - How does my app recognize that aud1 went through the event "audioPlayerDidFinishPlaying"? Is the delegate thing causes it to work as I intend without any further instructions? – Ohad Regev Oct 16 '11 at 12:24
  • That's correct. An AVAudioPlayer will send a message to its delegate when playback completes (note: this does not include paused audio). In my example code I set the delegate to whatever object was instantiating the players, but you could create a separate delegate if that makes more sense in your case. Note that whatever you use as your delegate will need to implement the AVAudioPlayerDelegate protocol. You can [find out a little more about delegates here](http://stackoverflow.com/questions/1045803/how-does-a-delegate-work-in-objective-c). – David Brainer Oct 18 '11 at 16:17