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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值