gpt4 book ai didi

ios - 我可以使用 AVAudioPlayer 播放带有振动的声音吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:44 27 4
gpt4 key购买 nike

我让 AVAudioPlayer 在聊天期间播放消息提醒的声音,我还想让手机振动。是否可以在 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()

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

振动:

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

振动在 Audiotoolbox.framework 中,播放声音在 AVFoundation.framework 中。

请注意,某些设备 (iPod Touch) 无法振动,这不会简单地起作用。

关于ios - 我可以使用 AVAudioPlayer 播放带有振动的声音吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28111877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com