Ios AVAudioPlayer缓慢降低液位

Ios AVAudioPlayer缓慢降低液位,ios,objective-c,avaudioplayer,Ios,Objective C,Avaudioplayer,我创造了一种声音,我这样演奏: NSURL *musicFile = [[NSBundle mainBundle] URLForResource:@"My music" withExtension:@"mp3"]; self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile

我创造了一种声音,我这样演奏:

NSURL *musicFile = [[NSBundle mainBundle] URLForResource:@"My music"
                                           withExtension:@"mp3"];
self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile
                                                              error:nil];
self.backgroundMusic.numberOfLoops = -1;
[self.backgroundMusic play];
但是当我改变观点时,我想停止音乐。但是慢慢地,平稳地,不是马上。当您将应用程序置于后台时,您的音乐不会立即停止,而是大约在0.5秒内停止

可能吗


谢谢

创建计时器并减少AVAudioPlayer的音量

[NSTimer scheduledTimerWithTimeInterval: 1.0
                      target: self
                      selector:@selector(timerTriggered:)
                      userInfo: nil repeats:YES];

-(void)timerTriggered:(NSTimer *)timer
{
  [appSoundPlayer setVolume: appSoundPlayer.volume - 0.1];

  if(appSoundPlayer.volume == 0.0)
  { 
    [timer inValidate];

    timer = nil;
  }
}

是的,这是可能的,有几种方法可以实现这种“淡出”效果

我在早期的一个项目中就是这样做的,对我来说效果非常好

-(void)fadeOut {  

    if (self.backgroundMusic.volume > 0.05) {

        self.backgroundMusic.volume = self.backgroundMusic.volume - 0.05;

        // Volume will be zero after ~0.5 seconds
        [self performSelector:@selector(fadeOut) withObject:nil afterDelay:1.0/40.0];   

     } else {
        [self.backgroundMusic stop];
    }
}