Audio description - SwiftUI
In SwiftUI, you can integrate AVPlayer
which has support of adding 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 composition = AVMutableComposition()
guard let videoUrl = Bundle.main.url(
forResource: "Appt",
withExtension: "mp4"
) else {
return
}
let videoAsset = AVURLAsset.init(url: videoUrl)
// Add video track to composition
if let videoTrack = try await videoAsset.loadTracks(withMediaType: .video).first,
let videoCompositionTrack = composition.addMutableTrack(
withMediaType: .video,
preferredTrackID: kCMPersistentTrackID_Invalid
) {
do {
try await videoCompositionTrack.insertTimeRange(
CMTimeRange(start: .zero, duration: videoAsset.load(.duration)),
of: videoTrack,
at: .zero
)
} catch { }
}
// Find and add the audio description track
for track in try await videoAsset.load(.tracks) {
if try await track.load(.mediaCharacteristics).contains(.describesVideoForAccessibility) {
if let audioCompositionTrack = composition.addMutableTrack(
withMediaType: track.mediaType,
preferredTrackID: kCMPersistentTrackID_Invalid
) {
do {
try await audioCompositionTrack.insertTimeRange(
CMTimeRange(start: .zero, duration: videoAsset.load(.duration)),
of: track,
at: .zero
)
} catch { }
break
}
}
}