ASAudioWaveformView
示例
要运行示例项目,请先克隆仓库,然后从示例目录运行 pod install
要求
swift 5.0+
安装
ASAudioWaveformView 可通过 CocoaPods 获取。要安装它,只需在 Podfile 中添加以下行
pod 'ASAudioWaveformView'
用法
有两种创建波形视图的方式,波形将自动调整以适应框架
-
初始化时设置框架。完成是可选的
let wave = ASAudioWaveformView.create(frame: CGRect(x: 0, y: 40, width: 200, height: 100)) { (config) in let url = Bundle.main.url(forResource: "test", withExtension: "mp3") config.audioURL(url).maxSamplesCount(500).fillColor(.systemTeal) } completion: { (empty) in print("-->draw Complete ,empty: \(empty)") }
-
如果你使用自动布局或在稍后设置框架。完成是可选的
let wave = ASAudioWaveformView() wave.createWaveform { (config) in let url = Bundle.main.url(forResource: "test", withExtension: "mp3") config.audioURL(url).positionType(.top).fillColor(.green) } completion: { (empty) in print("-->draw Complete ,empty: \(empty)") }
配置波形
/// config waveform postion, the default is center
public func positionType(_ type: ASAudioWaveformView.PositionType) -> ASAudioWaveformConfig
/// config waveform content style, the default is polyline
public func contentType(_ type: ASAudioWaveformView.ContentType) -> ASAudioWaveformConfig
/// config waveform fill color, the default is yellow
public func fillColor(_ color: UIColor) -> ASAudioWaveformConfig
/// config waveform audio source
public func audioURL(_ URL: URL?) -> ASAudioWaveformConfig
/// config max samples count, the default is 1000
public func maxSamplesCount(_ count: Int) -> ASAudioWaveformConfig
/// Specifies a range of time that may limit the temporal portion of the receiver's asset from which media data will be read.The default value of timeRange is CMTimeRangeMake(kCMTimeZero, kCMTimePositiveInfinity).
public func timeRange(_ range: CMTimeRange) -> ASAudioWaveformConfig
使用不同的音频URL重新加载
波形将自动调整以适应框架
/// Refresh waveform by audio url
public func refreshWaveform(with audioURL: URL?)
作者
Andrew Shen, [email protected]
许可证
ASAudioWaveformView可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。