AVPlayerViewController-Subtitles 是一个在 iOS 上显示字幕的库。它构建为一个 Swift 扩展,并且很容易集成。
如何开始
使用 CocoaPods 进行安装
platform :ios, '8.0'
pod 'AVPlayerViewController-Subtitles'
手动安装
下载(右键点击)并将其添加到您的项目中。
系统要求
版本 | 语言 | 最小iOS目标 |
---|---|---|
1.2.x | Swift 3.0 | iOS 8 |
1.0.x | Swift 2.x | iOS 8 |
与播放器一起使用
import AVPlayerViewControllerSubtitles
// Video file
let videoFile = Bundle.main.path(forResource: "trailer_720p", ofType: "mov")
// Subtitle file
let subtitleFile = Bundle.main.path(forResource: "trailer_720p", ofType: "srt")
let subtitleURL = URL(fileURLWithPath: subtitleFile!)
// Movie player
let moviePlayer = AVPlayerViewController()
moviePlayer.player = AVPlayer(url: URL(fileURLWithPath: videoFile!))
present(moviePlayer, animated: true, completion: nil)
// Add subtitles
moviePlayer.addSubtitles().open(file: subtitleURL)
moviePlayer.addSubtitles().open(file: subtitleURL, encoding: String.Encoding.utf8)
// Change text properties
moviePlayer.subtitleLabel?.textColor = UIColor.red
// Play
moviePlayer.player?.play()
屏幕截图
不与播放器一起使用
从版本 1.2 开始,您可以在不播放任何文件的情况下搜索 SubRip 文件或文本。
import AVPlayerViewControllerSubtitles
// Subtitle file
let subtitleFile = Bundle.main.path(forResource: "trailer_720p", ofType: "srt")
let subtitleURL = URL(fileURLWithPath: subtitleFile!)
// Subtitle parser
let parser = Subtitles(file: subtitleURL, encoding: .utf8)
// Do something with result
let subtitles = parser.searchSubtitles(at: 2.0) // Search subtitle at 2.0 seconds
联系
许可证
遵循Apache License v2.0许可。
版权所有2017年Marc Hervera。