.package(url: "https://github.com/excitedcosmos/ECWavingHaptics.git", from: "1.0.0")
或者
https://github.com/excitedcosmos/ECWavingHaptics.git
guard let url = Bundle.main.url(forResource: "Sound-File-Name", withExtension: "mp3") else {
print("Unknown Audio File")
return
}
let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 2, interleaved: false)!
let haptics = ECWavingHaptics(
audioFileURL: url,
audioFormat: audioFormat,
minFrequency: 150,
maxFrequency: 220,
isLooping: true,
playCallback: { print("Start Playing") },
stopCallback: { print("Stopped") },
errorCallback: { error in print("Error:\(error)") }
)
参数说明:
audioFileURL
: 音频文件的 URL(统一资源定位符)audioFormat
: 音频格式,包括采样率和通道数minFrequency
: 最小频率,单位为 Hz(赫兹)maxFrequency
: 最大频率,单位为 Hz(赫兹)isLooping
: 是否循环播放playCallback
: 开始播放时的回调函数stopCallback
: 停止播放时的回调函数errorCallback
: 发生错误时的回调函数haptics.startAudioProcessing()
haptics.stopAudioProcessing()
deinit {
haptics.stopAudioProcessing()
}
创建 ECWavingHaptics
实例
guard let url = Bundle.main.url(forResource: "Sound-File-Name", withExtension: "mp3") else {
print("Unknown Audio File")
return
}
let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 2, interleaved: false)!
let haptics = ECWavingHaptics(
audioFileURL: url,
audioFormat: audioFormat,
minFrequency: 150,
maxFrequency: 220,
isLooping: true,
playCallback: { print("Start Playing") },
stopCallback: { print("Stopped") },
errorCallback: { error in print("Error:\(error)") }
)
参数说明:
audioFileURL
: 音频文件的 URL(统一资源定位符)audioFormat
: 音频格式,包括采样率和通道数minFrequency
: 最小频率,单位为 Hz(赫兹)maxFrequency
: 最大频率,单位为 Hz(赫兹)isLooping
: 是否循环播放playCallback
: 开始播放时的回调函数stopCallback
: 停止播放时的回调函数errorCallback
: 发生错误时的回调函数开始音频处理和触觉反馈
haptics.startAudioProcessing()
停止音频处理和触觉反馈
haptics.stopAudioProcessing()
在适当的时机释放资源
deinit {
haptics.stopAudioProcessing()
}