【问题标题】:Add fading effect in songs playlist using AVAudioPlayer and MPMusicPlayerController in ios在 ios 中使用 AVAudioPlayer 和 MPMusicPlayerController 在歌曲播放列表中添加淡入淡出效果
【发布时间】:2012-12-14 04:34:22
【问题描述】:

我需要在歌曲播放列表中添加淡入/淡出功能(播放列表歌曲从 ipod 库中检索)。 当一首歌曲结束时我需要淡出效果并且下一首歌曲开始我需要淡入效果 另一个认为在衰落系统或媒体 VOLUME 不应该改变。

我正在使用 MPMusicPlayerController 播放歌曲。

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    这是我编写的 AVAudioPlayer 扩展(ARC 之前),它完全符合您的要求。

    标头AVAudioPlayer+FadeControl.h:

    #import <Foundation/Foundation.h>
    #import <AVFoundation/AVFoundation.h>
    
    
    @interface AVAudioPlayer (FadeControl)
    
    -(void)fadeOutWithDuration:(NSTimeInterval)inFadeOutTime;
    
    @end
    

    还有来源AVAudioPlayer+FadeControl.m

    #import "AVAudioPlayer+FadeControl.h"
    
    #define     kFadeSteps  20.0
    
    @implementation AVAudioPlayer (FadeControl)
    
    -(void)fadeOutWithDuration:(NSTimeInterval)inFadeOutTime
    {
        NSTimeInterval fireInterval = inFadeOutTime/kFadeSteps;
        float volumeDecrement = 1.0/kFadeSteps;
        float originalVolume = self.volume;
    
        self.volume = originalVolume - volumeDecrement;
    
        [self retain];
        [NSTimer scheduledTimerWithTimeInterval:fireInterval target:self selector:@selector(fadeOutTimerMethod:) userInfo:[NSNumber numberWithFloat:originalVolume] repeats:YES];
    
    }
    
    - (void)fadeOutTimerMethod:(NSTimer*)theTimer   
    {
        float volumeDecrement = 1.0/kFadeSteps;
    
        if (self.volume > volumeDecrement)
        {
            self.volume = self.volume - volumeDecrement;
        }
        else if ( [theTimer isValid] )
        {
            [self stop];
    
            NSNumber* originalVolume = [theTimer userInfo];
            self.volume = [originalVolume floatValue];
    
            [theTimer invalidate];
        }
        else 
        {
            [self stop];
            [self release];
        }
    
    
    }
    
    @end
    

    添加playWithFadeInDuration: 方法应该很简单。要在 ARC 项目中使用,只需将 -fno-objc-arc 添加到文件的编译标志。

    【讨论】:

    • 此方法有效,但它会降低系统音量,因此对我没有帮助 请告诉我如何在不更改系统音量的情况下降低音量(淡出)。什么是self.volume??????????
    • 同样的通用方法适用于MPMusicPlayerController
    • 它正在工作,但它会减少系统或媒体播放器的音乐,这不是正确的方法....我想在不增加/减少系统音量的情况下进行淡入淡出。再次感谢您的回复
    【解决方案2】:
    -(void)doFadeInVolume { 
    
    //play
    [audioPlayerMain play];
    
    if (audioPlayerMain.volume < 1.0) {
    audioPlayerMain.volume = audioPlayerMain.volume + 0.1;
    [self performSelector:@selector(doVolumeFadeMainIn) withObject:nil afterDelay:0.2];
    } 
    }
    

    并且可以使用相反的方法来淡出音量。希望这会有所帮助。

    【讨论】:

    • @PratikPatel 是的,这是假设您使用的是AVAudioPlayer。在头文件(即接口文件)中,您可以将其声明为AVAudioPlayer *audioPlayerMain,就像我在上面的示例中所做的那样。
    • same as @Pratik Patel 我如何使用 MPMusicPlayerController 完成它?????
    猜你喜欢
    • 2023-01-19
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多