SwiftAudio 0.11.2

SwiftAudio 0.11.2

Jørgen Henrichsen 维护。



SwiftAudio 0.11.2

  • Jørgen Henrichsen

logo

SwiftAudio

Build Status Version codecov License Platform

SwiftAudio 是一个用 Swift 编写的音频播放器,使得音频流的播放和文件播放更加简单。

示例

要查看音频播放器的工作情况,运行示例项目!要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install

要求

iOS 10.0+

安装

CocoaPods

SwiftAudio 可通过 CocoaPods 使用。要安装,只需将以下行添加到您的 Podfile 中即可

pod 'SwiftAudio', '~> 0.11.2'

Carthage

SwiftAudio 支持 Carthage。将以下内容添加到您的 Cartfile 中

github "jorgenhenrichsen/SwiftAudio" ~> 0.11.2

然后按照 添加框架 的 Carthage 指令进行操作。

用法

AudioPlayer

要开始播放音频,请按照以下步骤操作

let player = AudioPlayer()
let audioItem = DefaultAudioItem(audioUrl: "someUrl", sourceType: .stream)
player.load(item: audioItem, playWhenReady: true) // Load the item and start playing when the player is ready.

要监听 AudioPlayer 中的事件,请订阅位于 AudioPlayerevent 属性中找到的事件。要订阅一个事件

class MyCustomViewController: UIViewController {

    let audioPlayer = AudioPlayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        audioPlayer.event.stateChange.addListener(self, handleAudioPlayerStateChange)
    }
    
    func handleAudioPlayerStateChange(state: AudioPlayerState) {
        // Handle the event
    }
}

QueuedAudioPlayer

QueuedAudioPlayerAudioPlayer 的一个子类,它可以维护一个音频轨道队列。

let player = QueuedAudioPlayer()
let audioItem = DefaultAudioItem(audioUrl: "someUrl", sourceType: .stream)
player.add(item: audioItem, playWhenReady: true) // Since this is the first item, we can supply playWhenReady: true to immedietaly start playing when the item is loaded.

当播放一首曲目完成后,如果 automaticallyPlayNextSong 设置为 true(默认值),则播放器将加载下一首曲目并更新队列。

导航队列

所有 AudioItem 都存储在 previousItemsnextItems 中,分别表示在 currentItem 之前和之后的项目。使用以下方法导航队列

player.next() // Increments the queue, and loads the next item.
player.previous() // Decrements the queue, and loads the previous item.
player.jumpToItem(atIndex:) // Jumps to a certain item and loads that item.
操作队列
 player.removeItem(at:) // Remove a specific item from the queue.
 player.removeUpcomingItems() // Remove all items in nextItems.

配置 AudioPlayer

当前配置 AudioPlayer 的选项

  • bufferDuration:玩家需要缓冲的秒数。
  • timeEventFrequency:玩家调用代理时间进度事件的频率。
  • automaticallyWaitsToMinimizeStalling:指示玩家是否应自动延迟播放以最小化卡顿。
  • 音量
  • 静音
  • 速率
  • audioTimePitchAlgorithm:此值决定每个 AudioItem 所使用的 AVAudioTimePitchAlgorithm。在你的 AudioItem 子类中实现 TimePitching 以对每个 AudioItem 进行单独覆盖。

音频会话

请确保为您的应用激活一个合适的音频会话类别。这可以通过 AudioSessionController 实现。

try? AudioSessionController.shared.set(category: .playback)
//...
// You should wait with activating the session until you actually start playback of audio.
// This is to avoid interrupting other audio without the need to do it.
try? AudioSessionController.shared.activateSession()

重要:如果您想让应用处于非活动状态时音频继续播放,请记住激活后台音频:设置 -> 功能 -> 后台模式 -> 选择“音频、AirPlay 和画中画”。

中断

如果您使用 AudioSessionController 来设置音频会话,您也可以用它来处理中断。实现 AudioSessionControllerDelegate,您将被 handleInterruption(type: AVAudioSessionInterruptionType) 通知。如果您在应用退出时存储播放时间进度,在中断时这样做也是一个好主意。要将中断通知禁用,请将 isObservingForInterruptions 设置为 false

正在播放信息

AudioPlayer 可以自动为您更新 现在播放信息。这需要将 automaticallyUpdateNowPlayingInfo 设置为 true(默认值),并且传入的 AudioItem 必须为获取器返回值。AudioPlayer 将更新:艺术家、标题、专辑、封面、已播放时间、持续时间、速率。

可以通过访问 nowPlayingInfoController 的设置器来设置项目的附加属性。

    let player = AudioPlayer()
    player.load(item: someItem)
    player.nowPlayingInfoController.set(keyValue: NowPlayingInfoProperty.isLiveStream(true))

set(keyValue:)set(keyValues:]) 接受 MediaItemPropertyNowPlayingInfoProperty

可以通过强制从 AudioPlayer 重新加载/更新 info

    audioPlayer.loadNowPlayingMetaValues()
    audioPlayer.updateNowPlayingPlaybackValues()

可以通过以下方法清除当前信息

    audioPlayer.nowPlayingInfoController.clear()

远程命令

要为播放器启用远程命令,需要为播放器填充RemoteCommands数组。

audioPlayer.remoteCommands = [
    .play,
    .pause,
    .skipForward(intervals: [30]),
    .skipBackward(intervals: [30]),
  ]

这些命令将会对每个AudioItem生效。如果您需要某些音频项目使用不同的命令,请在自定义的AudioItem子类中实现RemoteCommandable。这些命令将覆盖AudioPlayer.remoteCommands中找到的命令,所以请确保为该特定AudioItem提供所有需要的命令。

为远程命令提供自定义处理程序

只需覆盖播放器中的RemoteCommandController中包含的处理程序,即可为您的远程命令提供自定义处理程序。

let player = QueuedAudioPlayer()
player.remoteCommandController.handlePlayCommand = { (event) in
    // Handle remote command here.
}

所有可用的覆盖可以通过查看RemoteCommandController找到。

从特定时间点开始播放

使您的AudioItem子类符合InitialTiming以从特定时间开始播放。

作者

Jørgen Henrichsen

许可

SwiftAudio可在MIT许可下获取。有关更多信息,请参阅LICENSE文件。