GoSwiftyM3U8 1.0.0

GoSwiftyM3U8 1.0.0

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最新发布2017年12月
SwiftSwift 版本4.0
SPM支持 SPM

Gal Orlanczyk 维护。



  • Gal Orlanczyk

Build Status
badge-language
badge-platforms
badge-license
badge-cocoapods
badge-documentation

GoSwiftyM3U8

M3U8 框架,用于解析和处理 .m3u8 索引文件

安装

Cocoapods

在您的 Podfile 中使用以下类似的内容(根据情况可能需要进行一些调整)。

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'GoSwiftyM3U8', '~> 1.0.0'

之后,在 Podfile 所在目录的终端中运行 pod install(有关 CocoaPods 的安装和使用细节,请访问它的官方网站)。

可以存在多个具有不同 Swift 版本的平台,这可能会迫使您在 Podfile 中添加 post install 处理。

使用指南

有关技术文档,请参阅 API
以下是一些基本的用法示例,使用此框架主要有两种方式

  1. 自行获取播放列表文本并仅使用解析器。
  2. 使用 M3U8Manager 获取并解析播放列表(如果需要,可以提供自定义播放列表获取器)。
  • 另一种选项是使用 PlaylistOperation 和自己的队列,并为所需获取的播放列表编写逻辑。

使用 M3U8Parser

解析播放列表的一个简单示例

let playlist = /* your playlist text from local/remote content */
let baseUrl = /* base url for the provided playlist */
let playlistType = /* the type of the playlist, can be: master/video/audio/subtitles */
let parser = M3U8Parser()
let params = M3U8Parser.Params(playlist: playlist, playlistType: .master, baseUrl: baseUrl)
let extraParams = M3U8Parser.ExtraParams(customRequiredTags: nil, extraTypes: nil, linePostProcessHandler: nil) // optional
do {
    let playlistResult = try parser.parse(params: params, extraParams: extraParams)
    // if our playlist was of type master you can unwrap the result like this:
    if case let .master(masterPlaylist) = playlistResult else {
        // use masterPlaylist
    }
} catch {
    // handle error
}            

附加信息

  • 解析器是同步的,这意味着如果您想在后台队列上使用它,您需要处理它。
  • 解析器可以被取消,但由于它是同步的,为了能够取消对 parse() 的调用,它必须是异步的。
  • 您可以使用解析器对象同步地解析多个列表。

使用 M3U8Manager

M3U8Manager 可以获取和解析单个播放列表或多个播放列表(对于多个,必须是媒体播放列表,因为必须先获取主播放列表才能获取媒体播放列表)。
您还可以使用 cancel() 取消所有管理任务。

单个播放列表处理

let manager = M3U8Manager()
let playlistFetcher = /* You can use a custom playlist fetcher or nil to use the default one */
let params = PlaylistOperation.Params(fetcher: playlistFetcher, url: playlistUrl, playlistType: playlistType)
let parserExtraParams = M3U8Parser.ExtraParams(customRequiredTags: nil, extraTypes: nil, linePostProcessHandler: nil) // optional
let extraParams = PlaylistOperation.ExtraParams(parser: parserExtraParams) // optional
let operationData = M3U8Manager.PlaylistOperationData(params: params, extraParams: extraParams)
let playlistType = /* the type of the playlist from the result (can be MasterPlaylist.self/MediaPlaylist.self) */
manager.fetchAndParsePlaylist(from: operationData, playlistType: playlistType) { (result) in
    switch result {
    case .success(let playlist):
        // handle playlist
    case .failure(let error): // handle the error
    case .cancelled: // handle cancelled
    }
}

多个播放列表

let manager = M3U8Manager()
let playlistFetcher = /* You can use a custom playlist fetcher or nil to use the default one */

let params1 = PlaylistOperation.Params(fetcher: playlistFetcher, url: firstUrl, playlistType: playlistType)
let parserExtraParams1 = M3U8Parser.ExtraParams(customRequiredTags: nil, extraTypes: nil, linePostProcessHandler: nil)
let extraParams1 = PlaylistOperation.ExtraParams(parser: parserExtraParams1)
let operationData1 = M3U8Manager.PlaylistOperationData(params: params1, extraParams: extraParams1)
        
let params2 = PlaylistOperation.Params(fetcher: playlistFetcher, url: secondUrl, playlistType: playlistType)
let parserExtraParams2 = M3U8Parser.ExtraParams(customRequiredTags: nil, extraTypes: nil, linePostProcessHandler: nil)
let extraParams2 = PlaylistOperation.ExtraParams(parser: parserExtraParams2)
let operationData2 = M3U8Manager.PlaylistOperationData(params: params2, extraParams: extraParams2)

let operationsData = [operationData1, operationData2]
manager.fetchAndParseMediaPlaylists(from: operationsData) { (result) in
    switch result {
    case .success(let playlists):
        // handle playlists
    case .failure(let error): // handle the error
    case .cancelled: // handle cancelled
    }
}