- //
- // AudioUtil.m
- // iaiai
- // QQ:176291935
- //
- // Created by iaiai on 14-9-25.
- // Copyright (c) 2014年 boying. All rights reserved.
- //
-
- #import "AudioUtil.h"
- #import <AudioToolbox/AudioToolbox.h>
-
- @implementation AudioUtil
-
- int number = 0;
-
- /**
- * 播放声音
- * @param name 资源名称
- * @param ext 资源后缀名
- **/
- -(void)audioPlay:(NSString*)name ext:(NSString*)ext{
- [self audioPlay:name ext:ext num:1];
- }
-
- /**
- * 播放声音
- * @param name 资源名称
- * @param ext 资源后缀名
- * @param num 循环次数
- **/
- -(void)audioPlay:(NSString*)name ext:(NSString*)ext num:(NSInteger*)num{
- NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:ext];
- SystemSoundID soundID;
- AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
- AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, audioPlayFinish,num);
- AudioServicesPlaySystemSound(soundID);
- number++;
- }
-
- /**
- * 震动
- **/
- -(void)vibratePlay{
- [self vibratePlay:1];
- }
-
- /**
- * 震动
- * @param num 震动次数
- **/
- -(void)vibratePlay:(NSInteger*)num{
- AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, audioPlayFinish,num);
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- number++;
- }
-
- /**
- * 播放完成之后的回调方法
- * @param soundID 播放的声音ID
- * @param num 播放次数
- **/
- void audioPlayFinish(SystemSoundID soundID,NSInteger* num){
- if (num&&num>0) {
- if (number>=num) {
- if (soundID!=kSystemSoundID_Vibrate) {
- AudioServicesDisposeSystemSoundID(soundID);
- }
- }else{
- AudioServicesPlaySystemSound(soundID);
- number++;
- }
- }else{
- if (soundID!=kSystemSoundID_Vibrate) {
- AudioServicesDisposeSystemSoundID(soundID);
- }
- }
- }
-
- @end
|