如果在系统设置中关闭字幕,则不会显示字幕
Subtitles won't display if closed caption is off in system settings
无论设备在辅助功能下设置了什么,我都在尝试显示字幕。目前,如果设备设置为英语并在设置中启用了隐藏式字幕,则会播放英语字幕,如果设备设置为西班牙语,则会播放西班牙语字幕。无论是否打开隐藏式字幕,我都希望播放字幕。
我试图从 apple 的文档中添加 this code,但没有帮助。它似乎可以很好地阅读选项,同时检测到西班牙语和英语字幕,但我无法让它们显示在屏幕上。
import UIKit
import AVFoundation
import AVKit
class ViewController : UIViewController
{
override func loadView()
{
let view = UIView()
view.backgroundColor = .white
self.view = view
}
override func viewDidLoad()
{
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "Lebron", ofType:"m4v")
else
{
debugPrint("File not Found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
player.appliesMediaSelectionCriteriaAutomatically = false
let asset = AVAsset(url: URL(fileURLWithPath: path))
for characteristic in asset.availableMediaCharacteristicsWithMediaSelectionOptions {
print("\(characteristic)")
// Retrieve the AVMediaSelectionGroup for the specified characteristic.
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: characteristic) {
// Print its options.
for option in group.options {
print(" Option: \(option.displayName)")
}
}
}
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristic.legible) {
let locale = Locale(identifier: "en")
let options =
AVMediaSelectionGroup.mediaSelectionOptions(from: group.options, with: locale)
if let option = options.first {
// Select Spanish-language subtitle option
player.currentItem!.select(option, in: group)
print("\(player.currentItem!.select(option, in: group))")
}
}
let playerViewController = AVPlayerViewController()
playerViewController.player = player
addChild(playerViewController)
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
playerViewController.player?.seek(to: CMTime(seconds: 0, preferredTimescale: 1))
}
}
这是输出到控制台的内容
2019-05-30 21:52:21.432911-0400 subtitleTest[6438:1450671] [Accessibility] ****************** Loading GAX Client Bundle ****************
AVMediaCharacteristic(_rawValue: AVMediaCharacteristicAudible)
Option: Unknown language
AVMediaCharacteristic(_rawValue: AVMediaCharacteristicLegible)
Option: Spanish
Option: Spanish Forced
Option: English
Option: English Forced
()
设备应显示与输入语言环境匹配的字幕文件,但它根本不显示任何字幕。
我最终成功了。这是我最终使用的代码。只需要将 LebronTestProdNamed
更改为您的视频文件的名称,并将 .m4v
更改为您的视频文件的文件类型。
import UIKit
import AVFoundation
import AVKit
class ViewController : UIViewController
{
override func loadView()
{
let view = UIView()
view.backgroundColor = .white
self.view = view
}
var test = AVPlayerMediaSelectionCriteria()
override func viewDidLoad()
{
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "LebronTestProdNamed", ofType:"m4v")
else
{
debugPrint("File not Found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
player.appliesMediaSelectionCriteriaAutomatically = false
let asset = AVAsset(url: URL(fileURLWithPath: path))
let playerItem = AVPlayerItem(asset: asset)
for characteristic in asset.availableMediaCharacteristicsWithMediaSelectionOptions {
print("\(characteristic)")
// Retrieve the AVMediaSelectionGroup for the specified characteristic.
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: characteristic) {
// Print its options.
for option in group.options {
print(" Option: \(option.displayName)")
}
}
}
// Create a group of the legible AVMediaCharacteristics (Subtitles)
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristic.legible) {
// Set the locale identifier to the value of languageID to select the current language
let locale = Locale(identifier: "en-EN")
// Create an option group to hold the options in the group that match the locale
let options =
AVMediaSelectionGroup.mediaSelectionOptions(from: group.options, with: locale)
// Assign the first option from options to the variable option
if let option = options.first {
// Select the option for the selected locale
playerItem.select(option, in: group)
}
}
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.player?.replaceCurrentItem(with: playerItem)
addChild(playerViewController)
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
playerViewController.player?.seek(to: CMTime(seconds: 0, preferredTimescale: 1))
}
}
无论设备在辅助功能下设置了什么,我都在尝试显示字幕。目前,如果设备设置为英语并在设置中启用了隐藏式字幕,则会播放英语字幕,如果设备设置为西班牙语,则会播放西班牙语字幕。无论是否打开隐藏式字幕,我都希望播放字幕。
我试图从 apple 的文档中添加 this code,但没有帮助。它似乎可以很好地阅读选项,同时检测到西班牙语和英语字幕,但我无法让它们显示在屏幕上。
import UIKit
import AVFoundation
import AVKit
class ViewController : UIViewController
{
override func loadView()
{
let view = UIView()
view.backgroundColor = .white
self.view = view
}
override func viewDidLoad()
{
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "Lebron", ofType:"m4v")
else
{
debugPrint("File not Found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
player.appliesMediaSelectionCriteriaAutomatically = false
let asset = AVAsset(url: URL(fileURLWithPath: path))
for characteristic in asset.availableMediaCharacteristicsWithMediaSelectionOptions {
print("\(characteristic)")
// Retrieve the AVMediaSelectionGroup for the specified characteristic.
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: characteristic) {
// Print its options.
for option in group.options {
print(" Option: \(option.displayName)")
}
}
}
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristic.legible) {
let locale = Locale(identifier: "en")
let options =
AVMediaSelectionGroup.mediaSelectionOptions(from: group.options, with: locale)
if let option = options.first {
// Select Spanish-language subtitle option
player.currentItem!.select(option, in: group)
print("\(player.currentItem!.select(option, in: group))")
}
}
let playerViewController = AVPlayerViewController()
playerViewController.player = player
addChild(playerViewController)
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
playerViewController.player?.seek(to: CMTime(seconds: 0, preferredTimescale: 1))
}
}
这是输出到控制台的内容
2019-05-30 21:52:21.432911-0400 subtitleTest[6438:1450671] [Accessibility] ****************** Loading GAX Client Bundle ****************
AVMediaCharacteristic(_rawValue: AVMediaCharacteristicAudible)
Option: Unknown language
AVMediaCharacteristic(_rawValue: AVMediaCharacteristicLegible)
Option: Spanish
Option: Spanish Forced
Option: English
Option: English Forced
()
设备应显示与输入语言环境匹配的字幕文件,但它根本不显示任何字幕。
我最终成功了。这是我最终使用的代码。只需要将 LebronTestProdNamed
更改为您的视频文件的名称,并将 .m4v
更改为您的视频文件的文件类型。
import UIKit
import AVFoundation
import AVKit
class ViewController : UIViewController
{
override func loadView()
{
let view = UIView()
view.backgroundColor = .white
self.view = view
}
var test = AVPlayerMediaSelectionCriteria()
override func viewDidLoad()
{
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "LebronTestProdNamed", ofType:"m4v")
else
{
debugPrint("File not Found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
player.appliesMediaSelectionCriteriaAutomatically = false
let asset = AVAsset(url: URL(fileURLWithPath: path))
let playerItem = AVPlayerItem(asset: asset)
for characteristic in asset.availableMediaCharacteristicsWithMediaSelectionOptions {
print("\(characteristic)")
// Retrieve the AVMediaSelectionGroup for the specified characteristic.
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: characteristic) {
// Print its options.
for option in group.options {
print(" Option: \(option.displayName)")
}
}
}
// Create a group of the legible AVMediaCharacteristics (Subtitles)
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristic.legible) {
// Set the locale identifier to the value of languageID to select the current language
let locale = Locale(identifier: "en-EN")
// Create an option group to hold the options in the group that match the locale
let options =
AVMediaSelectionGroup.mediaSelectionOptions(from: group.options, with: locale)
// Assign the first option from options to the variable option
if let option = options.first {
// Select the option for the selected locale
playerItem.select(option, in: group)
}
}
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.player?.replaceCurrentItem(with: playerItem)
addChild(playerViewController)
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
playerViewController.player?.seek(to: CMTime(seconds: 0, preferredTimescale: 1))
}
}