我可以使用AVAudioPlayer播放振动声音吗?

Kex*_*Kex 2 avaudioplayer ios

我有AVAudioPlayer在聊天期间播放消息提示音,我也想让手机振动.是否可以在AVAudioPlayer中执行此操作或者是否需要使用其他方法?

谢谢

Kli*_*akM 5

播放声音:

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];
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

Swift 3.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()
Run Code Online (Sandbox Code Playgroud)

振动:

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)

振动进入Audiotoolbox.framework并播放声音AVFoundation.framework.

请注意,某些设备(iPod Touch)不能振动,这不会简单地工作.