AVAudioPlayer fade volume out

前端 未结 13 2059
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 12:07

I have an AVAudioPlayer playing some audio (duh!)

The audio is initiated when the user presses a button. When they release it I want the audio to fade out.

I

13条回答
  •  抹茶落季
    2020-12-12 12:30

    In Objective-C try this:

    NSURL *trackURL = [[NSURL alloc]initFileURLWithPath:@"path to audio track"];
    
    // fade duration in seconds
    NSTimeInterval fadeDuration = 0.3;
    
    // duration of the audio track in seconds
    NSTimeInterval audioTrackDuration = 5.0; 
    
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]  initWithContentsOfURL:trackURL error:nil];
    [audioPlayer play];
    
    // first we set the volume to 1 - highest
    [audioPlayer setVolume:1.0]; 
    
    // then to 0 - lowest
    [musicAudioPlayer setVolume:0 fadeDuration:audioTrackDuration - fadeDuration];
    

提交回复
热议问题