diff --git a/ios/Video/Features/RCTPlayerOperations.swift b/ios/Video/Features/RCTPlayerOperations.swift index 320e3223..27d085c9 100644 --- a/ios/Video/Features/RCTPlayerOperations.swift +++ b/ios/Video/Features/RCTPlayerOperations.swift @@ -12,11 +12,13 @@ enum RCTPlayerOperations { static func setSideloadedText(player:AVPlayer?, textTracks:[TextTrack]?, criteria:SelectedTrackCriteria?) { let type = criteria?.type let textTracks:[TextTrack]! = textTracks ?? RCTVideoUtils.getTextTrackInfo(player) - + let trackCount:Int! = player?.currentItem?.tracks.count ?? 0 + // The first few tracks will be audio & video track - let firstTextIndex:Int = 0 - for firstTextIndex in 0..<(player?.currentItem?.tracks.count ?? 0) { - if player?.currentItem?.tracks[firstTextIndex].assetTrack?.hasMediaCharacteristic(.legible) ?? false { + var firstTextIndex:Int = 0 + for i in 0..<(trackCount) { + if player?.currentItem?.tracks[i].assetTrack?.hasMediaCharacteristic(.legible) ?? false { + firstTextIndex = i break } } @@ -24,7 +26,8 @@ enum RCTPlayerOperations { var selectedTrackIndex:Int = RCTVideoUnset if (type == "disabled") { - // Do nothing. We want to ensure option is nil + // Select the last text index which is the disabled text track + selectedTrackIndex = trackCount - firstTextIndex } else if (type == "language") { let selectedValue = criteria?.value as? String for i in 0.. NSURL! { + static func urlFilePath(filepath:NSString!, searchPath:FileManager.SearchPathDirectory) -> NSURL! { if filepath.contains("file://") { return NSURL(string: filepath as String) } // if no file found, check if the file exists in the Document directory - let paths:[String]! = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) + let paths:[String]! = NSSearchPathForDirectoriesInDomains(searchPath, .userDomainMask, true) var relativeFilePath:String! = filepath.lastPathComponent // the file may be multiple levels below the documents directory - let fileComponents:[String]! = filepath.components(separatedBy: "Documents/") + let directoryString:String! = searchPath == .cachesDirectory ? "Library/Caches/" : "Documents"; + let fileComponents:[String]! = filepath.components(separatedBy: directoryString) if fileComponents.count > 1 { relativeFilePath = fileComponents[1] } @@ -192,6 +193,7 @@ enum RCTVideoUtils { static func getValidTextTracks(asset:AVAsset, assetOptions:NSDictionary?, mixComposition:AVMutableComposition, textTracks:[TextTrack]?) -> [TextTrack] { let videoAsset:AVAssetTrack! = asset.tracks(withMediaType: AVMediaType.video).first var validTextTracks:[TextTrack] = [] + if let textTracks = textTracks, textTracks.count > 0 { for i in 0.. TextTrack? { + let fileManager = FileManager.default + let cachesDirectoryUrl = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0] + let filePath = cachesDirectoryUrl.appendingPathComponent("empty.vtt").path + + if !fileManager.fileExists(atPath: filePath) { + let stringToWrite = "WEBVTT\n\n1\n99:59:59.000 --> 99:59:59.001\n." + + do { + try stringToWrite.write(to: URL(fileURLWithPath: filePath), atomically: true, encoding: String.Encoding.utf8) + } catch { + return nil + } + } + + return TextTrack([ + "language": "disabled", + "title": "EmptyVttFile", + "type": "text/vtt", + "uri": filePath, + ]) + } + static func delay(seconds: Int = 0) -> Promise { return Promise(on: .global()) { fulfill, reject in DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(seconds)) / Double(NSEC_PER_SEC), execute: { diff --git a/ios/Video/RCTVideoManager.swift b/ios/Video/RCTVideoManager.swift index bf42f707..d1c73de8 100644 --- a/ios/Video/RCTVideoManager.swift +++ b/ios/Video/RCTVideoManager.swift @@ -5,7 +5,7 @@ import React class RCTVideoManager: RCTViewManager { override func view() -> UIView { - return RCTVideo(eventDispatcher: bridge.eventDispatcher()) + return RCTVideo(eventDispatcher: bridge.eventDispatcher() as! RCTEventDispatcher) } func methodQueue() -> DispatchQueue {