我可以使用 AVAudioPlayer 播放带有振动的声音吗?
Can I play a sound with vibrate using AVAudioPlayer?
我让 AVAudioPlayer 在聊天期间播放消息提醒的声音,我还希望 phone 振动。是否可以在 AVAudioPlayer 中执行此操作,还是我需要使用其他方法?
谢谢
要播放声音:
NSString *source = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"mp3"];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:source] error:nil];
self.audioPlayer.delegate = self;
self.audioPlayer.numberOfLoops = 0;
self.audioPlayer.volume = 1.0;
[self.audioPlayer play];
Swift 2.2版本:
var source = NSBundle.mainBundle().pathForResource("beep", ofType: "mp3")!
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL.fileURLWithPath(source))
}
catch let error {
// error handling
}
audioPlayer.delegate = self
audioPlayer.numberOfLoops = 0
audioPlayer.volume = 1.0
audioPlayer.play()
Swift3.0版本:
var source = Bundle.main.path(forResource: "beep", ofType: "mp3")!
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: URL(fileURLWithPath: source))
}
catch {
// error handling
}
audioPlayer.delegate = self
audioPlayer.numberOfLoops = 0
audioPlayer.volume = 1.0
audioPlayer.play()
振动:
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
震动在Audiotoolbox.framework
,播放声音在AVFoundation.framework
。
请注意,某些设备 (iPod Touch) 无法振动,这不会简单地起作用。
我让 AVAudioPlayer 在聊天期间播放消息提醒的声音,我还希望 phone 振动。是否可以在 AVAudioPlayer 中执行此操作,还是我需要使用其他方法?
谢谢
要播放声音:
NSString *source = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"mp3"];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:source] error:nil];
self.audioPlayer.delegate = self;
self.audioPlayer.numberOfLoops = 0;
self.audioPlayer.volume = 1.0;
[self.audioPlayer play];
Swift 2.2版本:
var source = NSBundle.mainBundle().pathForResource("beep", ofType: "mp3")!
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL.fileURLWithPath(source))
}
catch let error {
// error handling
}
audioPlayer.delegate = self
audioPlayer.numberOfLoops = 0
audioPlayer.volume = 1.0
audioPlayer.play()
Swift3.0版本:
var source = Bundle.main.path(forResource: "beep", ofType: "mp3")!
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: URL(fileURLWithPath: source))
}
catch {
// error handling
}
audioPlayer.delegate = self
audioPlayer.numberOfLoops = 0
audioPlayer.volume = 1.0
audioPlayer.play()
振动:
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
震动在Audiotoolbox.framework
,播放声音在AVFoundation.framework
。
请注意,某些设备 (iPod Touch) 无法振动,这不会简单地起作用。