如果在系统设置中关闭了隐藏字幕,则不会显示字幕

时间:2019-05-31 02:02:09

标签: ios swift avfoundation avplayer avkit

无论设备在辅助功能下设置了什么,我都试图显示字幕。当前,如果设备设置为英语且在设置中启用了隐藏字幕,则将播放英语字幕,如果设备设置为西班牙语,则将播放西班牙语字幕。无论隐藏式字幕是否打开,我都希望播放字幕。

我试图从Apple的文档中添加this code,但没有帮助。似乎可以很好地阅读这些选项,可以同时检测西班牙语和英语字幕,但是我无法在屏幕上显示它们。

mFragmentTransaction.hide(mFragments[toPos]);
//need change to  
mFragmentTransaction.hide(mFragments[fromPos]);

这是输出到控制台的内容

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

    }
}

设备应显示与输入语言环境匹配的任何字幕文件,但它根本不显示任何字幕。

1 个答案:

答案 0 :(得分:0)

我最终成功了。这是我最终使用的代码。只需将ConvertB更改为视频文件的名称,并将ConvertA更改为视频文件的文件类型。

LebronTestProdNamed