project_files/HedgewarsMobile/Classes/MXAudioPlayerFadeOperation.m
changeset 8441 a00b0fa0dbd7
parent 6908 896ed2afcfb8
child 11137 14f50dde3e8c
equal deleted inserted replaced
8440:ea4d6a7a2937 8441:a00b0fa0dbd7
     8 #import <AVFoundation/AVFoundation.h>
     8 #import <AVFoundation/AVFoundation.h>
     9 
     9 
    10 #define SKVolumeChangesPerSecond 15
    10 #define SKVolumeChangesPerSecond 15
    11 
    11 
    12 @interface MXAudioPlayerFadeOperation ()
    12 @interface MXAudioPlayerFadeOperation ()
    13 @property (nonatomic, retain, readwrite) AVAudioPlayer *audioPlayer; 
    13 @property (nonatomic, retain, readwrite) AVAudioPlayer *audioPlayer;
    14 - (void)beginFadeOperation;
    14 - (void)beginFadeOperation;
    15 - (void)finishFadeOperation;
    15 - (void)finishFadeOperation;
    16 @end
    16 @end
    17 
    17 
    18 @implementation MXAudioPlayerFadeOperation
    18 @implementation MXAudioPlayerFadeOperation
    85     [self beginFadeOperation];
    85     [self beginFadeOperation];
    86   }
    86   }
    87   else {
    87   else {
    88     ALog(@"AudioPlayerFadeOperation began with invalid AVAudioPlayer");
    88     ALog(@"AudioPlayerFadeOperation began with invalid AVAudioPlayer");
    89   }
    89   }
    90   
    90 
    91   [pool release];
    91   [pool release];
    92 }
    92 }
    93 
    93 
    94 - (void)beginFadeOperation {
    94 - (void)beginFadeOperation {
    95   if (![self.audioPlayer isPlaying] && _playBeforeFade) [self.audioPlayer play];
    95   if (![self.audioPlayer isPlaying] && _playBeforeFade) [self.audioPlayer play];
    96   
    96 
    97   if (_fadeDuration != 0.0) {
    97   if (_fadeDuration != 0.0) {
    98     
    98 
    99     NSTimeInterval sleepInterval = (1.0 / SKVolumeChangesPerSecond);
    99     NSTimeInterval sleepInterval = (1.0 / SKVolumeChangesPerSecond);
   100     NSTimeInterval startTime = [[NSDate date] timeIntervalSinceReferenceDate];
   100     NSTimeInterval startTime = [[NSDate date] timeIntervalSinceReferenceDate];
   101     NSTimeInterval now = startTime;
   101     NSTimeInterval now = startTime;
   102     
   102 
   103     float startVolume = [self.audioPlayer volume];
   103     float startVolume = [self.audioPlayer volume];
   104     
   104 
   105     while (now < (startTime + _fadeDuration)) {
   105     while (now < (startTime + _fadeDuration)) {
   106       float ratioOfFadeCompleted = (now - startTime)/_fadeDuration;
   106       float ratioOfFadeCompleted = (now - startTime)/_fadeDuration;
   107       float volume = (_finishVolume * ratioOfFadeCompleted) + (startVolume * (1-ratioOfFadeCompleted));
   107       float volume = (_finishVolume * ratioOfFadeCompleted) + (startVolume * (1-ratioOfFadeCompleted));
   108       [self.audioPlayer setVolume:volume];
   108       [self.audioPlayer setVolume:volume];
   109       [NSThread sleepForTimeInterval:sleepInterval];
   109       [NSThread sleepForTimeInterval:sleepInterval];
   110       now = [[NSDate date] timeIntervalSinceReferenceDate];
   110       now = [[NSDate date] timeIntervalSinceReferenceDate];
   111     }
   111     }
   112     
   112 
   113     [self.audioPlayer setVolume:_finishVolume];
   113     [self.audioPlayer setVolume:_finishVolume];
   114     [self finishFadeOperation];
   114     [self finishFadeOperation];
   115   }
   115   }
   116   else {
   116   else {
   117     [self.audioPlayer setVolume:_finishVolume];
   117     [self.audioPlayer setVolume:_finishVolume];