Jukebox 0.2.1

Jukebox 0.2.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布上次发布2016 年 10 月
SPM支持 SPM

Teodor Patraș 维护。



Jukebox 0.2.1

Jukebox: audio player in Swift

Jukebox 是用 Swift 编写的 iOS 音频播放器。

内容

  1. 功能
  2. 安装
  3. 支持的操作系统和 SDK 版本
  4. 用法
  5. 处理远程事件
  6. 公开接口
  7. 委托
  8. 许可证
  9. 联系方式

功能

  • [x] 同时支持流式传输远程和本地音频文件
  • [x] 支持流式传输实时音频流
  • [x] 支持执行 playpausestopreplayplay nextplay previous控制音量 和将播放位置移动到特定秒的功能。
  • [x] 与 MPNowPlayingInfoCenter 集成后台模式

安装

手动安装

如果您不希望使用上述任何一个依赖管理器,您可以将 Jukebox 手动集成到项目中。

支持的操作系统和 SDK 版本

  • iOS 8.0+
  • Xcode 7+

用法

先决条件

  • 为了支持后台模式,请在您的 Info.plist 中添加以下内容
<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>
  • 如果您想从 http:// URLs 流式传输,请在您的 Info.plist 中添加以下内容
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
        <true/>
</dict>

入门

1) 创建一个 Jukebox 实例

// configure jukebox
jukebox = Jukebox(delegate: self, items: [
    JukeboxItem(URL: NSURL(string: "http://www.noiseaddicts.com/samples_1w72b820/2514.mp3")!),
    JukeboxItem(URL: NSURL(string: "http://www.noiseaddicts.com/samples_1w72b820/2958.mp3")!)
    ])

2) 播放并享受

jukebox?.play()

处理远程事件

为了处理远程事件,您需要执行以下操作

  • 首先,您需要调用以接收远程事件

UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

  • 其次,重写 remoteControlReceivedWithEvent(event:)
override func remoteControlReceived(with event: UIEvent?) {
    if event?.type == .remoteControl {
        switch event!.subtype {
        case .remoteControlPlay :
            jukebox.play()
        case .remoteControlPause :
            jukebox.pause()
        case .remoteControlNextTrack :
            jukebox.playNext()
        case .remoteControlPreviousTrack:
            jukebox.playPrevious()
        case .remoteControlTogglePlayPause:
            if jukebox.state == .playing {
               jukebox.pause()
            } else {
                jukebox.play()
            }
        default:
            break
        }
    }
}

公共接口

公共方法

/**
 Starts item playback.
*/
public func play()

/**
Plays the item indicated by the passed index

 - parameter index: index of the item to be played
*/
public func play(atIndex index: Int)

/**
 Pauses the playback.
*/
public func pause()

/**
 Stops the playback.
*/
public func stop()

/**
 Starts playback from the beginning of the queue.
*/
public func replay()

/**
 Plays the next item in the queue.
*/
public func playNext()

/**
 Restarts the current item or plays the previous item in the queue
*/
public func playPrevious()

/**
 Restarts the playback for the current item
*/
public func replayCurrentItem()

/**
 Seeks to a certain second within the current AVPlayerItem and starts playing

 - parameter second: the second to seek to
 - parameter shouldPlay: pass true if playback should be resumed after seeking
*/
public func seek(toSecond second: Int, shouldPlay: Bool = false)

/**
 Appends and optionally loads an item

 - parameter item:            the item to be appended to the play queue
 - parameter loadingAssets:   pass true to load item's assets asynchronously
*/
public func append(item: JukeboxItem, loadingAssets: Bool)

/**
 Removes an item from the play queue

 - parameter item: item to be removed
*/
public func remove(item: JukeboxItem)

/**
 Removes all items from the play queue matching the URL

 - parameter url: the item URL
*/
public func removeItems(withURL url : URL)

公共属性

属性 类型 描述
volume Float 玩家的音量
currentItem JukeboxItem 封装当前播放项目元数据的对象

代理

Jukebox 定义了一个委托协议,如果您想通知自定义事件,可以使用此协议

public protocol JukeboxDelegate: class {
    func jukeboxStateDidChange(_ state : Jukebox)
    func jukeboxPlaybackProgressDidChange(_ jukebox : Jukebox)
    func jukeboxDidLoadItem(_ jukebox : Jukebox, item : JukeboxItem)
    func jukeboxDidUpdateMetadata(_ jukebox : Jukebox, forItem: JukeboxItem)
}

许可证

Jukebox 在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE 文件。

联系方式

您可以在我的 Twitter 账户 teodorpatras 上关注或留言。如果您在此项目中发现问题,可以创建工单。也欢迎提出拉取请求。