游戏音乐音效添加管理

//

//  GameAudioManager.h

//  MyProduct

//

//  Created by 苹果 on 13-8-15.

//

//


#import <Foundation/Foundation.h>

#import <AVFoundation/AVFoundation.h>


@interface GameAudioManager : NSObject


+ (void)addAllSound;

+ (void)addSoundAtPath:(NSString *)filePath forKey:(id)key;

+ (void)playSoundForKey:(id)key;


+ (void)addAudioAtPath:(NSString *)filePath forKey:(id)key;


+ (void)playAudioForKey:(id)key fadeInInterval:(NSTimeInterval)fadeInInterval;

+ (void)playAudioForKey:(id)key;


+ (void)stopAudioForKey:(id)key fadeOutInterval:(NSTimeInterval)fadeOutInterval;

+ (void)stopAudioForKey:(id)key;


+ (void)pauseAudioForKey:(id)key fadeOutInterval:(NSTimeInterval)fadeOutInterval;

+ (void)pauseAudioForKey:(id)key;


+ (AVAudioPlayer *)audioPlayerForKey:(id)key;


@end




//

//  GameAudioManager.m

//  MyProduct

//

//  Created by 苹果 on 13-8-15.

//

//


#import "GameAudioManager.h"

#import <AudioToolbox/AudioToolbox.h>


#define GAMEAUDIOMANAGE_AUDIO_FADE_STEPS   2


@implementation GameAudioManager {

    NSMutableDictionary *_sounds;

    NSMutableDictionary *_audio;

}


//TODO 硬编 讲所有的音效和音乐添加到游戏中

+ (void)addAllSound{

    

    

}


+ (GameAudioManager *)sharedInstance

{

    __strong static id _sharedObject = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _sharedObject = [[self alloc] init];

    });

    return _sharedObject;

}


- (id)init

{

    self = [super init];

    if (self != nil) {

        _sounds = [NSMutableDictionary dictionary];

        _audio = [NSMutableDictionary dictionary];

    }

    return self;

}


- (void)addSoundAtPath:(NSString *)filePath forKey:(id)key

{

    NSURL* fileURL = [NSURL fileURLWithPath:filePath];

    SystemSoundID soundId;

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &soundId);

    

    [_sounds setObject:[NSNumber numberWithInt:soundId] forKey:key];

}


+ (void)addSoundAtPath:(NSString *)filePath forKey:(id)key

{

    [[self sharedInstance] addSoundAtPath:filePath forKey:key];

}


- (void)playSoundForKey:(id)key

{

    SystemSoundID soundId = [(NSNumber *)[_sounds objectForKey:key] intValue];

    AudioServicesPlaySystemSound(soundId);

}


+ (void)playSoundForKey:(id)key

{

    [[self sharedInstance] playSoundForKey:key];

}


- (void)addAudioAtPath:(NSString *)filePath forKey:(id)key

{

    NSURL* fileURL = [NSURL fileURLWithPath:filePath];

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];

    [_audio setObject:player forKey:key];

}


+ (void)addAudioAtPath:(NSString *)filePath forKey:(id)key

{

    [[self sharedInstance] addAudioAtPath:filePath forKey:key];

}


- (void)fadeIn:(NSTimer *)timer

{

    AVAudioPlayer *player = timer.userInfo;

    float volume = player.volume;

    volume = volume + 1.0 / GAMEAUDIOMANAGE_AUDIO_FADE_STEPS;

    volume = volume > 1.0 ? 1.0 : volume;

    player.volume = volume;

    

    if (volume == 1.0) {

        [timer invalidate];

    }

}


- (void)playAudioForKey:(id)key fadeInInterval:(NSTimeInterval)fadeInInterval

{

    AVAudioPlayer *player = [_audio objectForKey:key];

    

    // If fade in inteval interval is not 0, schedule fade in

    if (fadeInInterval > 0.0) {

        player.volume = 0.0;

        NSTimeInterval interval = fadeInInterval / GAMEAUDIOMANAGE_AUDIO_FADE_STEPS;

        [NSTimer scheduledTimerWithTimeInterval:interval

                                         target:self

                                       selector:@selector(fadeIn:)

                                       userInfo:player

                                        repeats:YES];

    }

    

    [player play];

}


+ (void)playAudioForKey:(id)key fadeInInterval:(NSTimeInterval)fadeInInterval

{

    [[self sharedInstance] playAudioForKey:key fadeInInterval:fadeInInterval];

}


+ (void)playAudioForKey:(id)key

{

    [[self sharedInstance] playAudioForKey:key fadeInInterval:0.0];

}



- (void)fadeOutAndStop:(NSTimer *)timer

{

    AVAudioPlayer *player = timer.userInfo;

    float volume = player.volume;

    volume = volume - 1.0 / GAMEAUDIOMANAGE_AUDIO_FADE_STEPS;

    volume = volume < 0.0 ? 0.0 : volume;

    player.volume = volume;

    

    if (volume == 0.0) {

        [timer invalidate];

        [player pause];

    }

}


- (void)stopAudioForKey:(id)key fadeOutInterval:(NSTimeInterval)fadeOutInterval

{

    AVAudioPlayer *player = [_audio objectForKey:key];

    

    // If fade in inteval interval is not 0, schedule fade in

    if (fadeOutInterval > 0) {

        NSTimeInterval interval = fadeOutInterval / GAMEAUDIOMANAGE_AUDIO_FADE_STEPS;

        [NSTimer scheduledTimerWithTimeInterval:interval

                                         target:self

                                       selector:@selector(fadeOutAndStop:)

                                       userInfo:player

                                        repeats:YES];

    } else {

        [player stop];

    }

}


+ (void)stopAudioForKey:(id)key fadeOutInterval:(NSTimeInterval)fadeOutInterval

{

    [[self sharedInstance] stopAudioForKey:key fadeOutInterval:fadeOutInterval];

}


+ (void)stopAudioForKey:(id)key

{

    [[self sharedInstance] stopAudioForKey:key fadeOutInterval:0.0];

}



- (void)fadeOutAndPause:(NSTimer *)timer

{

    AVAudioPlayer *player = timer.userInfo;

    float volume = player.volume;

    volume = volume - 1.0 / GAMEAUDIOMANAGE_AUDIO_FADE_STEPS;

    volume = volume < 0.0 ? 0.0 : volume;

    player.volume = volume;

    

    if (volume == 0.0) {

        [timer invalidate];

        [player stop];

    }

}


- (void)pauseAudioForKey:(id)key fadeOutInterval:(NSTimeInterval)fadeOutInterval

{

    AVAudioPlayer *player = [_audio objectForKey:key];

    

    // If fade in inteval interval is not 0, schedule fade in

    if (fadeOutInterval > 0) {

        NSTimeInterval interval = fadeOutInterval / GAMEAUDIOMANAGE_AUDIO_FADE_STEPS;

        [NSTimer scheduledTimerWithTimeInterval:interval

                                         target:self

                                       selector:@selector(fadeOutAndPause:)

                                       userInfo:player

                                        repeats:YES];

    } else {

        [player pause];

    }

}



+ (void)pauseAudioForKey:(id)key fadeOutInterval:(NSTimeInterval)fadeOutInterval

{

    [[self sharedInstance] pauseAudioForKey:key fadeOutInterval:fadeOutInterval];

}


+ (void)pauseAudioForKey:(id)key

{

    [[self sharedInstance] pauseAudioForKey:key fadeOutInterval:0.0];

}



- (AVAudioPlayer *)audioPlayerForKey:(id)key

{

    return [_audio objectForKey:key];

}


+ (AVAudioPlayer *)audioPlayerForKey:(id)key

{

    return [[self sharedInstance] audioPlayerForKey:key];

}


@end


//调用方式

@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

        

    [MCSoundBoard addSoundAtPath:[[NSBundle mainBundle] pathForResource:@"ding.wav" ofType:nil] forKey:@"ding"];

    

    [MCSoundBoard addAudioAtPath:[[NSBundle mainBundle] pathForResource:@"play.mp3" ofType:nil] forKey:@"play"];

    

    [MCSoundBoard addAudioAtPath:[[NSBundle mainBundle] pathForResource:@"loop.mp3" ofType:nil] forKey:@"loop"];

    AVAudioPlayer *player = [MCSoundBoard audioPlayerForKey:@"loop"];

    player.numberOfLoops = -1// Endless

    [MCSoundBoard playAudioForKey:@"loop" fadeInInterval:2.0];

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


- (IBAction)playSound:(id)sender

{

    [MCSoundBoard playSoundForKey:@"ding"];

}


- (IBAction)playAudio:(id)sender

{

    [MCSoundBoard playAudioForKey:@"play"];

}


- (IBAction)loopAudio:(id)sender

{

    AVAudioPlayer *player = [MCSoundBoard audioPlayerForKey:@"loop"];

    if (player.playing) {

        [MCSoundBoard pauseAudioForKey:@"loop" fadeOutInterval:2.0];

    } else {

        [MCSoundBoard playAudioForKey:@"loop" fadeInInterval:2.0];

    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值