iOS: How to Have a Certain Action Occur for a Specific Time Duration?
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 开发者_StackOverflow社区- 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?
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.
精彩评论