前言
extern void AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) __OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_2_0);
@available(iOS 2.0, *) public func AudioServicesPlayAlertSound(inSystemSoundID: SystemSoundID)
1、短频音效播放
-
Objective-C
// 添加库文件:AudioToolbox.framework
// 包含头文件:#import <AudioToolbox/AudioToolbox.h>
// 声明要保存音效文件的变量
SystemSoundID soundID;
// 加载文件
NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"音效" ofType:@"caf"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileURL), &soundID);
// 播放短频音效
AudioServicesPlayAlertSound(soundID);
// 增加震动效果,如果手机处于静音状态,提醒音将自动触发震动
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
-
Swift
// 添加库文件:AudioToolbox.framework
// 包含头文件:import AudioToolbox
// 声明要保存音效文件的变量
var soundID:SystemSoundID = 0
// 加载文件
let fileUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("音效", ofType: "caf")!)
AudioServicesCreateSystemSoundID(fileUrl, &soundID)
// 播放短频音效
AudioServicesPlayAlertSound(soundID)
// 增加震动效果,如果手机处于静音状态,提醒音将自动触发震动
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate));