Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Latest commit

 

History

History
47 lines (42 loc) · 1.85 KB

File metadata and controls

47 lines (42 loc) · 1.85 KB

Audio description - iOS

On iOS, AVPlayer has support to add audio description. Users can enable audio description automatically through System Preferences. Turning on audio description works automatically if you add the public.accessibility.describes-video property to the audio description track.

The code example below shows a basic implementation of enabling audio description embedded inside a video.

let videoComposition = AVMutableComposition()

// Add video track
guard let videoTrack = videoComposition.addMutableTrack(
    withMediaType: .video, 
    preferredTrackID: kCMPersistentTrackID_Invalid
) else { 
    return 
}
guard let videoUrl = Bundle.main.url(
    forResource: "Appt", 
    withExtension: "mp4"
) else { 
    return 
}
let videoAsset = AVURLAsset.init(url: videoUrl)
try? videoTrack.insertTimeRange(
    CMTimeRangeMake(start: .zero, duration: videoAsset.duration),
    of: videoAsset.tracks(withMediaType: .video)[0],
    at: .zero
)

// Find & add audio description track
for track in videoAsset.tracks {
    if track.hasMediaCharacteristic(.describesVideoForAccessibility) {
        guard let audioTrack = videoComposition.addMutableTrack(
            withMediaType: track.mediaType, 
            preferredTrackID: kCMPersistentTrackID_Invalid
        ) else { 
            return 
        }
        try? audioTrack.insertTimeRange(
            CMTimeRange(start: .zero, duration: videoAsset.duration), 
            of: track, 
            at: .zero
        )
        break
    }
}