Reputation: 83
Im am developing an iOS app in Xcode. I am still new to programming.
I am trying to set the volume of an audiotrack while playing it with an AVPlayer
. It is working great if a set the time at kCMTimeZero
but I want it to set the volume one second after the button was pressed.
Not working
- (IBAction)maxVolButtonPressed:(id)sender {
[audioInputParams1 setVolume:1 atTime:time1];
[self applyAudioMix];
}
Working
- (IBAction)minVolButtonPressed:(id)sender {
[audioInputParams1 setVolume:0 atTime:kCMTimeZero];
[self applyAudioMix];
}
What should I write after atTime
if I want a one second delay?
ANSWER: Ok so I figured it out. You have to add the time where the current item is at the moment. I use setVolumeRampFromStartVolume with a very little time interval instead of setVolume. setVolume fades to the given volume for some reason I haven't figured it out why. It works for me like this:
CMTime time1 = CMTimeMake(1000, 1000); //2s
CMTime time2 = CMTimeMake(1001, 1000); //3s
CMTime timeCurrent = [player currentTime];
CMTime time1Added = CMTimeAdd(timeCurrent, time1);
CMTime time2Added = CMTimeAdd(timeCurrent, time2);
CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(time1Added, time2Added);
[audioInputParams1 setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange:fadeInTimeRange];
[self applyAudioMix];
Upvotes: 1
Views: 1123
Reputation: 480
You should do something like this:
- (IBAction)maxVolButtonPressed:(id)sender {
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setVolume:) userInfo:nil repeats:NO];
}
- (void)setVolume:(id)sender{
[audioInputParams1 setVolume:1 atTime:kCMTimeZero];
[self applyAudioMix];
}
In first method you are saying: wait for 1 second and then call method setVolume:.
Upvotes: 1