本文介绍了可以使用AVAudioPlayer振动播放声音吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我让AVAudioPlayer在聊天期间播放声音以发出消息警报,我也希望手机振动.是否可以在AVAudioPlayer中执行此操作,或者我需要使用其他方法?
I have AVAudioPlayer playing a sound for a message alert during chat, I also want the phone to vibrate. Is it possible to do this in AVAudioPlayer or do I need to use a different method?
谢谢
推荐答案
播放声音:
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版本:
Swift 2.2 version:
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()
Swift 3.0版本:
Swift 3.0 version:
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
中有声音.
Vibration is in Audiotoolbox.framework
and playing a sound is in AVFoundation.framework
.
请注意,某些设备(iPod Touch)无法振动,因此无法正常工作.
Please note that some devices (iPod Touch) cannot vibrate and this won't simply work.
这篇关于可以使用AVAudioPlayer振动播放声音吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!