You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS使用Swift实现音频与振动同步启停的技术问询

iOS音频与振动同步实现方案

当然可以实现音频播放时同步振动、音频停止时振动也停止的效果!你当前的代码只在音频结束后触发振动,是因为没有在音频启动的同时启动振动逻辑,也没做持续振动的处理。下面是调整后的完整实现方案:

步骤1:导入必要框架

首先要导入处理音频和系统振动的核心框架:

import AVFoundation
import AudioToolbox

步骤2:定义全局变量

添加定时器变量来控制持续振动,同时保留原有的音频播放器变量:

var alertSoundEffect: AVAudioPlayer?
var vibrationTimer: Timer?

步骤3:实现同步播放与振动的核心方法

func playSoundAndVibrate() {
    // 安全获取音频文件路径
    guard let path = Bundle.main.path(forResource: "alert.mp3", ofType: nil) else {
        print("找不到alert.mp3文件,请检查文件是否添加到项目中")
        return
    }
    let url = URL(fileURLWithPath: path)
    
    do {
        alertSoundEffect = try AVAudioPlayer(contentsOf: url)
        // 设置代理监听音频播放状态
        alertSoundEffect?.delegate = self
        alertSoundEffect?.prepareToPlay()
        
        // 同步启动音频和振动
        alertSoundEffect?.play()
        startContinuousVibration()
    } catch {
        print("音频加载失败:\(error.localizedDescription)")
    }
}

// 启动持续振动:通过定时器重复触发系统振动
func startContinuousVibration() {
    // 先触发一次振动,保证和音频同时开始
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    
    // 每隔0.5秒重复振动(可根据音频节奏调整间隔,比如0.3秒让振动更密集)
    vibrationTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { _ in
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    }
}

// 停止振动并清理定时器
func stopVibration() {
    vibrationTimer?.invalidate()
    vibrationTimer = nil
}

步骤4:监听音频播放结束事件

通过AVAudioPlayerDelegate代理方法,精准捕获音频结束/出错的时机,自动停止振动:

extension YourViewController: AVAudioPlayerDelegate {
    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        // 音频播放完成,停止振动并清理资源
        stopVibration()
        alertSoundEffect = nil
    }
    
    func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
        // 音频解码出错,同样停止振动
        stopVibration()
        if let error = error {
            print("音频解码错误:\(error.localizedDescription)")
        }
        alertSoundEffect = nil
    }
}

关键说明

  • 为什么用定时器?因为系统默认的kSystemSoundID_Vibrate是单次短振动,要实现和音频同步的持续振动,需要通过定时器重复触发。你可以根据音频的节奏调整定时器的时间间隔,让振动更贴合音频节奏。
  • 一定要设置AVAudioPlayer的代理,这样才能精准捕获音频播放结束的时机,及时停止振动。
  • 记得在控制器销毁前调用stopVibration(),避免定时器内存泄漏。

内容的提问来源于stack exchange,提问作者Hafiz Shoaib Awan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:17:54