SwiftAudioEx 1.1.0

SwiftAudioEx 1.1.0

David Chavez 维护。



  • David Chavez 和 Jørgen Henrichsen 提供

logo

SwiftAudio

codecov License Platform

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

示例

要查看音频播放器的工作状态,运行示例项目!要运行示例项目,克隆仓库,然后在 Xcode 中打开 Example/SwiftAudio.xcodeproj。在 XCode 项目导航中选择“Example for SwiftAudio”,并在模拟器(或在实际设备上)中构建/运行它。

要求

iOS 11.0+

安装

Swift 包管理器

Swift 包管理器(SwiftPM)是一个工具,用于管理 Swift 代码和 C 家族依赖的分布。从 Xcode 11 开始,SwiftPM 与 Xcode 本地集成。

SwiftAudio 从 0.12.0 版本开始支持 SwiftPM。要使用 SwiftPM,您应使用 Xcode 11 打开项目。点击 文件 -> Swift 包 -> 添加包依赖,输入 SwiftAudio 仓库的 URL。或者,您可以使用 GitHub 账号登录 Xcode,并直接输入 SwiftAudio 以进行搜索。

选择包后,您可以选择依赖类型(标记版本、分支或提交)。然后 Xcode 将为您设置所有这些内容。

如果您是框架作者并使用 SwiftAudio 作为依赖项,请更新您的 Package.swift 文件

let package = Package(
    // 0.12.0 ..< 1.0.0
    dependencies: [
        .package(url: "https://github.com/DoubleSymmetry/SwiftAudio.git", from: "0.12.0")
    ],
    // ...
)

CocoaPods

SwiftAudio 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

pod 'SwiftAudioEx', '~> 0.15.3'

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
    }
}

队列音频播放器

队列音频播放器AudioPlayer的一个子类,它维护一个音频轨队列。

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.

当一个轨播放完毕时,播放器将加载下一个轨并更新队列。

导航队列

所有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:指示播放器是否应自动延迟播放以最小化停滞。
  • 音量
  • muted
  • 速率
  • audioTimePitchAlgorithm:此值决定每个AudioItem使用的AVAudioTimePitchAlgorithm。在你的AudioItem子类中实现TimePitching以单独为每个AudioItem重写。

特定于QueuedAudioPlayer的选项

  • repeatMode:重复模式:关闭、轨道、队列

音频会话

请记住为您的应用程序激活适当类别的音频会话。这可以通过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 可以自动更新 nowPlayingInfo。这需要将 automaticallyUpdateNowPlayingInfo 设置为 true(默认),并且传入的 AudioItem 必须为获取器返回值。AudioPlayer 将更新:艺术家、标题、专辑、艺术作品、已播时间、时长和速率。

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

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

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

可以强制从 AudioPlayer 重新加载/更新信息。

    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,以便能够从特定时间开始播放。

作者

乔根·亨利申

授权

SwiftAudio可在MIT授权下使用。查看LICENSE文件获取更多信息。