音效

-(void) initWithYinxiao{
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"清碎.wav" withExtension:nil];
    //1.获得系统声音ID
    SystemSoundID soundID=0;
    /**
     * inFileUrl:音频文件url
     * outSystemSoundID:声音id(此函数会将音效文件加入到系统音频服务中并返回一个长整形ID)
     */
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
    //如果需要在播放完之后执行某些操作,可以调用如下方法注册一个播放完成回调函数
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
    //2.播放音频
    AudioServicesPlaySystemSound(soundID);//播放音效
    //    AudioServicesPlayAlertSound(soundID);//播放音效并震动
    //3.销毁声音
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        AudioServicesDisposeSystemSoundID(soundID);
//    });
    //  AudioServicesDisposeSystemSoundID(soundID);
}
 [self initWithYinxiao];

 头文件 :

import <AudioToolbox/AudioToolbox.h>

猜你喜欢

转载自www.cnblogs.com/sheer-code/p/10732350.html