2022-05-19 07:29:25 -06:00
|
|
|
import AVFoundation
|
|
|
|
import MediaAccessibility
|
|
|
|
import Promises
|
|
|
|
|
|
|
|
let RCTVideoUnset = -1
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Collection of mutating functions
|
|
|
|
*/
|
|
|
|
enum RCTPlayerOperations {
|
|
|
|
|
|
|
|
static func setSideloadedText(player:AVPlayer?, textTracks:[TextTrack]?, criteria:SelectedTrackCriteria?) {
|
|
|
|
let type = criteria?.type
|
|
|
|
let textTracks:[TextTrack]! = textTracks ?? RCTVideoUtils.getTextTrackInfo(player)
|
2022-06-13 03:40:43 -06:00
|
|
|
let trackCount:Int! = player?.currentItem?.tracks.count ?? 0
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
// The first few tracks will be audio & video track
|
2022-06-13 03:40:43 -06:00
|
|
|
var firstTextIndex:Int = 0
|
|
|
|
for i in 0..<(trackCount) {
|
|
|
|
if player?.currentItem?.tracks[i].assetTrack?.hasMediaCharacteristic(.legible) ?? false {
|
|
|
|
firstTextIndex = i
|
2022-05-19 07:29:25 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var selectedTrackIndex:Int = RCTVideoUnset
|
|
|
|
|
|
|
|
if (type == "disabled") {
|
2022-06-13 03:40:43 -06:00
|
|
|
// Select the last text index which is the disabled text track
|
|
|
|
selectedTrackIndex = trackCount - firstTextIndex
|
2022-05-19 07:29:25 -06:00
|
|
|
} else if (type == "language") {
|
|
|
|
let selectedValue = criteria?.value as? String
|
|
|
|
for i in 0..<textTracks.count {
|
|
|
|
let currentTextTrack = textTracks[i]
|
|
|
|
if (selectedValue == currentTextTrack.language) {
|
|
|
|
selectedTrackIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type == "title") {
|
|
|
|
let selectedValue = criteria?.value as? String
|
|
|
|
for i in 0..<textTracks.count {
|
|
|
|
let currentTextTrack = textTracks[i]
|
|
|
|
if (selectedValue == currentTextTrack.title) {
|
|
|
|
selectedTrackIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type == "index") {
|
|
|
|
if let value = criteria?.value, let index = value as? Int {
|
|
|
|
if textTracks.count > index {
|
|
|
|
selectedTrackIndex = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// in the situation that a selected text track is not available (eg. specifies a textTrack not available)
|
|
|
|
if (type != "disabled") && selectedTrackIndex == RCTVideoUnset {
|
2022-06-13 03:40:43 -06:00
|
|
|
let captioningMediaCharacteristics = MACaptionAppearanceCopyPreferredCaptioningMediaCharacteristics(.user)
|
2022-05-19 07:29:25 -06:00
|
|
|
let captionSettings = captioningMediaCharacteristics as? [AnyHashable]
|
|
|
|
if ((captionSettings?.contains(AVMediaCharacteristic.transcribesSpokenDialogForAccessibility)) != nil) {
|
|
|
|
selectedTrackIndex = 0 // If we can't find a match, use the first available track
|
|
|
|
let systemLanguage = NSLocale.preferredLanguages.first
|
|
|
|
for i in 0..<textTracks.count {
|
|
|
|
let currentTextTrack = textTracks[i]
|
|
|
|
if systemLanguage == currentTextTrack.language {
|
|
|
|
selectedTrackIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-13 03:40:43 -06:00
|
|
|
|
|
|
|
for i in firstTextIndex..<(trackCount) {
|
2022-05-19 07:29:25 -06:00
|
|
|
var isEnabled = false
|
|
|
|
if selectedTrackIndex != RCTVideoUnset {
|
|
|
|
isEnabled = i == selectedTrackIndex + firstTextIndex
|
|
|
|
}
|
|
|
|
player?.currentItem?.tracks[i].isEnabled = isEnabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UNUSED
|
|
|
|
static func setStreamingText(player:AVPlayer?, criteria:SelectedTrackCriteria?) {
|
|
|
|
let type = criteria?.type
|
|
|
|
let group:AVMediaSelectionGroup! = player?.currentItem?.asset.mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristic.legible)
|
|
|
|
var mediaOption:AVMediaSelectionOption!
|
|
|
|
|
|
|
|
if (type == "disabled") {
|
|
|
|
// Do nothing. We want to ensure option is nil
|
|
|
|
} else if (type == "language") || (type == "title") {
|
|
|
|
let value = criteria?.value as? String
|
|
|
|
for i in 0..<group.options.count {
|
|
|
|
let currentOption:AVMediaSelectionOption! = group.options[i]
|
|
|
|
var optionValue:String!
|
|
|
|
if (type == "language") {
|
|
|
|
optionValue = currentOption.extendedLanguageTag
|
|
|
|
} else {
|
|
|
|
optionValue = currentOption.commonMetadata.map(\.value)[0] as! String
|
|
|
|
}
|
|
|
|
if (value == optionValue) {
|
|
|
|
mediaOption = currentOption
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//} else if ([type isEqualToString:@"default"]) {
|
|
|
|
// option = group.defaultOption; */
|
|
|
|
} else if (type == "index") {
|
|
|
|
if let value = criteria?.value, let index = value as? Int {
|
|
|
|
if group.options.count > index {
|
|
|
|
mediaOption = group.options[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // default. invalid type or "system"
|
|
|
|
#if TARGET_OS_TV
|
|
|
|
// Do noting. Fix for tvOS native audio menu language selector
|
|
|
|
#else
|
|
|
|
player?.currentItem?.selectMediaOptionAutomatically(in: group)
|
|
|
|
return
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TARGET_OS_TV
|
|
|
|
// Do noting. Fix for tvOS native audio menu language selector
|
|
|
|
#else
|
|
|
|
// If a match isn't found, option will be nil and text tracks will be disabled
|
|
|
|
player?.currentItem?.select(mediaOption, in:group)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static func setMediaSelectionTrackForCharacteristic(player:AVPlayer?, characteristic:AVMediaCharacteristic, criteria:SelectedTrackCriteria?) {
|
|
|
|
let type = criteria?.type
|
|
|
|
let group:AVMediaSelectionGroup! = player?.currentItem?.asset.mediaSelectionGroup(forMediaCharacteristic: characteristic)
|
|
|
|
var mediaOption:AVMediaSelectionOption!
|
2022-11-28 14:15:50 -07:00
|
|
|
|
|
|
|
guard group != nil else { return }
|
2022-05-19 07:29:25 -06:00
|
|
|
|
|
|
|
if (type == "disabled") {
|
|
|
|
// Do nothing. We want to ensure option is nil
|
|
|
|
} else if (type == "language") || (type == "title") {
|
|
|
|
let value = criteria?.value as? String
|
|
|
|
for i in 0..<group.options.count {
|
|
|
|
let currentOption:AVMediaSelectionOption! = group.options[i]
|
|
|
|
var optionValue:String!
|
|
|
|
if (type == "language") {
|
|
|
|
optionValue = currentOption.extendedLanguageTag
|
|
|
|
} else {
|
|
|
|
optionValue = currentOption.commonMetadata.map(\.value)[0] as? String
|
|
|
|
}
|
|
|
|
if (value == optionValue) {
|
|
|
|
mediaOption = currentOption
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//} else if ([type isEqualToString:@"default"]) {
|
|
|
|
// option = group.defaultOption; */
|
|
|
|
} else if type == "index" {
|
|
|
|
if let value = criteria?.value, let index = value as? Int {
|
|
|
|
if group.options.count > index {
|
|
|
|
mediaOption = group.options[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let group = group { // default. invalid type or "system"
|
|
|
|
player?.currentItem?.selectMediaOptionAutomatically(in: group)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let group = group {
|
|
|
|
// If a match isn't found, option will be nil and text tracks will be disabled
|
|
|
|
player?.currentItem?.select(mediaOption, in:group)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static func seek(player: AVPlayer, playerItem:AVPlayerItem, paused:Bool, seekTime:Float, seekTolerance:Float) -> Promise<Bool> {
|
|
|
|
let timeScale:Int = 1000
|
|
|
|
let cmSeekTime:CMTime = CMTimeMakeWithSeconds(Float64(seekTime), preferredTimescale: Int32(timeScale))
|
|
|
|
let current:CMTime = playerItem.currentTime()
|
|
|
|
let tolerance:CMTime = CMTimeMake(value: Int64(seekTolerance), timescale: Int32(timeScale))
|
|
|
|
|
|
|
|
return Promise<Bool>(on: .global()) { fulfill, reject in
|
|
|
|
guard CMTimeCompare(current, cmSeekTime) != 0 else {
|
2022-06-24 16:51:26 -06:00
|
|
|
reject(NSError(domain: "", code: 0, userInfo: nil))
|
2022-05-19 07:29:25 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !paused { player.pause() }
|
|
|
|
|
|
|
|
player.seek(to: cmSeekTime, toleranceBefore:tolerance, toleranceAfter:tolerance, completionHandler:{ (finished:Bool) in
|
|
|
|
fulfill(finished)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func configureAudio(ignoreSilentSwitch:String, mixWithOthers:String) {
|
2023-03-03 07:47:05 -07:00
|
|
|
let audioSession:AVAudioSession! = AVAudioSession.sharedInstance()
|
2022-05-19 07:29:25 -06:00
|
|
|
var category:AVAudioSession.Category? = nil
|
|
|
|
var options:AVAudioSession.CategoryOptions? = nil
|
2023-03-03 07:47:05 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
if (ignoreSilentSwitch == "ignore") {
|
|
|
|
category = AVAudioSession.Category.playback
|
|
|
|
} else if (ignoreSilentSwitch == "obey") {
|
|
|
|
category = AVAudioSession.Category.ambient
|
|
|
|
}
|
2023-03-03 07:47:05 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
if (mixWithOthers == "mix") {
|
|
|
|
options = .mixWithOthers
|
|
|
|
} else if (mixWithOthers == "duck") {
|
|
|
|
options = .duckOthers
|
|
|
|
}
|
2023-03-03 07:47:05 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
if let category = category, let options = options {
|
|
|
|
do {
|
2023-03-03 07:47:05 -07:00
|
|
|
try audioSession.setCategory(category, options: options)
|
2022-05-19 07:29:25 -06:00
|
|
|
} catch {
|
2023-03-03 07:47:05 -07:00
|
|
|
debugPrint("[RCTPlayerOperations] Problem setting up AVAudioSession category and options. Error: \(error).")
|
2023-04-05 14:50:22 -06:00
|
|
|
// Handle specific set category and option combination error
|
2023-03-03 07:47:05 -07:00
|
|
|
// setCategory:AVAudioSessionCategoryPlayback withOptions:mixWithOthers || duckOthers
|
|
|
|
// Failed to set category, error: 'what' Error Domain=NSOSStatusErrorDomain
|
|
|
|
// https://developer.apple.com/forums/thread/714598
|
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
do {
|
|
|
|
debugPrint("[RCTPlayerOperations] Reseting AVAudioSession category to playAndRecord with defaultToSpeaker options.")
|
|
|
|
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
|
|
|
|
} catch {
|
|
|
|
debugPrint("[RCTPlayerOperations] Reseting AVAudioSession category and options problem. Error: \(error).")
|
|
|
|
}
|
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
} else if let category = category, options == nil {
|
|
|
|
do {
|
2023-03-03 07:47:05 -07:00
|
|
|
try audioSession.setCategory(category)
|
2022-05-19 07:29:25 -06:00
|
|
|
} catch {
|
2023-03-03 07:47:05 -07:00
|
|
|
debugPrint("[RCTPlayerOperations] Problem setting up AVAudioSession category. Error: \(error).")
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
} else if category == nil, let options = options {
|
|
|
|
do {
|
2023-03-03 07:47:05 -07:00
|
|
|
try audioSession.setCategory(audioSession.category, options: options)
|
2022-05-19 07:29:25 -06:00
|
|
|
} catch {
|
2023-03-03 07:47:05 -07:00
|
|
|
debugPrint("[RCTPlayerOperations] Problem setting up AVAudioSession options. Error: \(error).")
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|