My code is as follows:
//init mix composition
let mixComposition = AVMutableComposition()
//video asset
let videoAsset = AVURLAsset(url: VIDEO_URL)
let videoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
try? videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: videoAsset.tracks(withMediaType: AVMediaTypeVideo).first!, at: kCMTimeZero)
//audio track
let audioTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
try? audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: videoAsset.tracks(withMediaType: AVMediaTypeAudio).first!, at: kCMTimeZero)
//subtitles
let path = Bundle.main.path(forResource: "subtitle", ofType: "vtt")!
let subtitleAsset = AVAsset(url: URL(fileURLWithPath: path))
let subtitleTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeText, preferredTrackID: kCMPersistentTrackID_Invalid)
try? subtitleTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: subtitleAsset.tracks(withMediaType: AVMediaTypeText)[0], at: kCMTimeZero)
//setup player and play
let item = AVPlayerItem(asset: videoAsset)
let player = AVPlayer(playerItem: item)
self.player = player
self.player?.allowsExternalPlayback = true
self.player?.usesExternalPlaybackWhileExternalScreenIsActive = true
self.player?.play()
When asked if the videoAsset.isCompatibleWithAirPlayVideo
it returns true. When you load the application it all works smoothly until you enter Airplay mode, the screen freezes until you go out of Airplay mode again.
This is completely different behaviour if you were to change mixComposition to videoAsset. Now it all works, but you won't have the external subtitles.
Did anyone else run into this problem and what the work-around. My end goal is to include external subtitles to mp4 video.