音频流 1.2.4

AudioStreaming 1.2.4

Dimitris C. 维护。



  • Dimitris C.

AudioStreaming CI

音频流

一个用Swift编写的iOS AudioPlayer/Stream库,允许播放在线音频流,本地文件以及无缝队列。

在内部,`AudioStreaming` 使用 `AVAudioEngine` 和 `CoreAudio` 进行播放,并提供了一种简单的应用实时 音频增强 的方法。

支持音频

  • 带有元数据解析的在线流媒体(Shoutcast/ICY流)
  • AIFF, AIFC, WAV, CAF, NeXT, ADTS, MPEG Audio Layer 3, AAC音频格式
  • M4A(《仅优化文件》)

已知限制

要求

  • iOS 12.0+
  • Swift 5.x

使用AudioStreaming

通过HTTP播放音频源

注:您需要保留对AudioPlayer对象的引用。

let player = AudioPlayer()
player.play(url: URL(string: "https://your-remote-url/to/audio-file.mp3")!)

播放本地文件

let player = AudioPlayer()
player.play(url: URL(fileURLWithPath: "your-local-path/to/audio-file.mp3")!)

队列音频文件

let player = AudioPlayer()
// when you want to queue a single url
player.queue(url: URL(string: "https://your-remote-url/to/audio-file.mp3")!)

// or if you want to queue a list of urls use
player.queue(urls: [
    URL(fileURLWithPath: "your-local-path/to/audio-file.mp3")!,
    URL(fileURLWithPath: "your-local-path/to/audio-file-2.mp3")!
])

调整播放属性

let player = AudioPlayer()
player.play(url: URL(fileURLWithPath: "your-local-path/to/audio-file.mp3")!)
// adjust the playback rate
player.rate = 2.0

// adjusting the volume
player.volume = 0.5

// mute/unmute the audio
player.mute = true

// pause the playback
player.pause()

// resume the playback
player.resume()

// stop the playback
player.stop()

// seeking to to a time (in seconds)
player.seek(to: 10)

音频播放属性

let player = AudioPlayer()
player.play(url: URL(fileURLWithPath: "your-local-path/to/audio-file.mp3")!)

// To get the audio file duration
let duration = player.duration

// To get the progress of the player
let progress = player.progress

// To get the state of the player, for possible values view the `AudioPlayerState` enum
let state = player.state

// To get the stop reason of the player, for possible values view the `AudioPlayerStopReason` enum
let state = player.stopReason

AudioPlayer代理

您可以通过使用AudioPlayerdelegate属性检查各种回调,以获得有关播放器状态、错误等信息。有关更多详细信息,请查看AudioPlayerDelegate

let player = AudioPlayer()
player.play(url: URL(fileURLWithPath: "your-local-path/to/audio-file.mp3")!)

player.delegate = self // an object conforming to AudioPlayerDelegate

// observing the audio player state, provides the new and previous state of the player.
func audioPlayerStateChanged(player: AudioPlayer, with newState: AudioPlayerState, previous: AudioPlayerState) {}

向AudioPlayer添加自定义音频节点

AudioStreaming提供了一种简单的方法来附加/删除AVAudioNode(s)。这提供了一种强大的方法,可以通过各种增强来调整播放音频。

let reverbNode = AVAudioUnitReverb()
reverbNode.wetDryMix = 50 

let player = AudioPlayer()
// attach a single node
player.attach(node: reverbNode)

// detach a single node
player.detach(node: reverbNode)

// detach all custom added nodes
player.detachCustomAttachedNodes()

示例项目演示了如何向AudioPlayer添加自定义AVAudioUnitEQ节点以为其添加均衡器。

添加自定义框架过滤器以记录和观察音频数据

AudioStreaming允许添加自定义框架过滤器,以便记录或观察正在播放的音频。

您可以通过使用AudioPlayerframeFiltering属性来添加框架过滤器。

let player = AudioPlayer()
let format = player.mainMixerNode.outputFormat(forBus: 0)

let settings = [
    AVFormatIDKey: kAudioFormatMPEG4AAC,
    AVSampleRateKey: format.sampleRate,
    AVNumberOfChannelsKey: format.channelCount
] as [String : Any]

var audioFile = try? AVAudioFile(
        forWriting: outputUrl,
        settings: settings,
        commonFormat: format.commonFormat,
        interleaved: format.isInterleaved)

let record = FilterEntry(name: "record") { buffer, when in
    try? audioFile?.write(from: buffer)
}

player.frameFiltering.add(entry: record)

有关添加和删除框架滤器的更多方法,请参阅FrameFiltering协议。观察过滤器时的回调将在主线程之外的线程上运行。

在内部,框架过滤器具体的类FrameFilterProcessor会将一个tap安装在AVAudioEnginemainMixerNode上,所有添加的过滤器都将从该tap中调用。

注意 由于mainMixerNode是公开暴露的,因此应特别注意不要直接安装一个tap,并且使用框架过滤器,这会导致异常,因为根据苹果的文档,只能在一个输出节点上安装一个tap。

安装

Cocoapods

...

$ gem install cocoapods

...

pod 'AudioStreaming'

...

在Xcode 11.0+中,您可以通过访问 文件 / Swift包 / 添加包依赖... 来添加新的依赖项,并输入包仓库URL https://github.com/dimitris-c/AudioStreaming.git。然后按照指示操作。

Carthage

Carthage 是一个去中心化的依赖项管理器,它构建您的依赖项并提供框架。

您可以使用以下命令通过Homebrew安装Carthage:

$ brew update
$ brew install carthage

要将AudioStreaming集成到您的Xcode项目中并使用Carthage,请将以下内容添加到您的 Cartfile

github "dimitris-c/AudioStreaming"

访问 Carthage的安装说明 来安装框架

许可证

AudioStreaming归MIT许可证管理。有关更多信息,请参阅LICENSE文件。

归属

这个库受到已经过测试的流媒体库 StreamingKit 的启发。向 Thong Nguyen (@tumtumtum) 和 Matt Gallagher (@mattgallagher) 的 AudioStreamer 表示衷心的感谢