Can I play a sound with vibrate using AVAudioPlayer?

社会主义新天地 提交于 2019-12-06 13:47:51

To play a sound:

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 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 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()

To vibrate:

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

Vibration is in Audiotoolbox.framework and playing a sound is in AVFoundation.framework.

Please note that some devices (iPod Touch) cannot vibrate and this won't simply work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!