IOS 声音处理

#import "SoundManager.h"

#import <Audiotoolbox/AudioServices.h>

#import <AudioToolbox/AudioToolbox.h>

#import <CoreAudio/CoreAudioTypes.h>

#import "SimpleAudioEngine.h"


static SoundManager *_manager = nil;

@implementation SoundManager


+(SoundManager*)manager{

    if(_manager == nil){

        _manager = [[SoundManager alloc] init];

        return _manager;

    }

    

    return _manager;

}


-(id)init{

    if(self = [super init]){

        isStopEff = false;

    }


    return self;

}




-(void)player:(NSString *)fileName{

    if(avaPlayer){

        if ([avaPlayer isPlaying]) {

            [avaPlayer stop];

        }

        [avaPlayer release];

    }

    

    if (timer != nil) {

        [timer invalidate];

        timer = nil;

    }

    

   NSString * path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"loading/%@",fileName] ofType:@"mp3"];

    

    NSURL *soundURL = [[NSURL alloc]initFileURLWithPath:path];

    NSError *error;

    avaPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:&error];

    NSLog(@"%@",error);


    [avaPlayer prepareToPlay];

    avaPlayer.numberOfLoops = -1;

    avaPlayer.volume = 0.3;

    [avaPlayer updateMeters];

    

    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSoundVolume) userInfo:nil repeats:YES];

    

    [avaPlayer play];


    

    [soundURL release];

}


-(void)updateSoundVolume{

    if (avaPlayer) {

        NSLog(@"%f",avaPlayer.volume);

        avaPlayer.volume = avaPlayer.volume + 0.1;

        [avaPlayer updateMeters];

        if(avaPlayer.volume >= 1){

            if (timer) {

                [timer invalidate];

                timer = nil;

            }

        }

    }else{

        if (timer != nil ) {

            [timer invalidate];

            timer = nil;

        }

    }

}


-(void)playerEffSound:(NSString *)fileName{

    if (!isStopEff && fileName) {

        NSString * path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"loading/%@",fileName] ofType:@"wav"];

        [[SimpleAudioEngine sharedEngine] playEffect:path];

    }

}


-(void)stop{

    if (avaPlayer) {

        [avaPlayer stop];

        if (timer != nil) {

            [timer invalidate];

            timer = nil;

        }

    }

}


-(void)stopEff{

    isStopEff = true;

}


-(void)restartEff{

    isStopEff = false;

}


-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{

    [player release];

}


-(void)dealloc{

    if (avaPlayer) {

        [avaPlayer release];

    }

    

    if (timer) {

        [timer invalidate];

        timer = nil;

    }

    

    [super dealloc];

}


//- (void)setDefaultAudioDevice

//{

//    UInt32 propertySize = 0;

//    OSStatus status = noErr;

//    AudioObjectPropertyAddress propertyAOPA;

//    

//    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;

//    propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;

//    propertyAOPA.mSelector = kAudioHardwarePropertyDefaultOutputDevice;

//    propertySize = sizeof(AudioDeviceID);

//    

//    status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAOPA, 0, NULL, &propertySize, &outputDeviceID);

//    

//    if(status) {

//        // Error

//        return;

//    }

//}


@end


你可能感兴趣的:(IOS 声音处理)