Re: Problems Fading Out Music
Re: Problems Fading Out Music
- Subject: Re: Problems Fading Out Music
- From: Graham Cox <email@hidden>
- Date: Thu, 7 Jan 2010 14:38:25 +1100
On 07/01/2010, at 2:23 PM, Chunk 1978 wrote:
> unfortunately i'm developing for iPhone OS (which i should have stated
> earlier) so NSAnimation doesn't seem to be an option :-/
So just use NSTimer, within or without a wrapper object of your own devise.
Here's the basics of the code I used, which implements a generic fade method. This is called for a number of different tasks, such as fading in, fading out and ducking the track. Warning: this is an old project, and I think I would do things slightly differently now, but the basic principle of using a repeating timer remains the way to go, IMO.
- (void) fadeFromLevel:(float) l1 toLevel:(float) l2 inTime:(NSTimeInterval) time
{
// performs a linear fade from l1 to l2 over the time interval <time>.
if ( _fader == nil )
{
DJFadeInfo* fi = [[DJFadeInfo alloc] initWithLevel1:l1 level2:l2 time:time];
_fader = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0
target:self
selector:@selector(fadeCallback:)
userInfo:fi
repeats:YES];
_fadeStartTime = [[NSDate date] retain];
}
}
- (void) fadeCallback:(NSTimer*) timer
{
//NSLog(@"fade target = %@", [self name]);
DJFadeInfo* fi = [timer userInfo];
NSTimeInterval t = [[NSDate date] timeIntervalSinceDate:_fadeStartTime];
float vol = [fi level1] + ([fi delta] * ( t / [fi time]));
if ( t > [fi time])
{
[_fader invalidate];
_fader = nil;
[_fadeStartTime release];
_fadeStartTime = nil;
[self setMainLevel:[fi level2]];
[fi release];
}
else
{
[self setMainLevel:vol];
}
}
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden