Skip to main content
Logo Appt Light

Captions on iOS

Captions should be provided to enable users to understand what is being said in videos. Captions are a text based alternative for media, such as videos. The spoken dialogue and sound effects are displayed on screen as the video plays. Captions mostly benefit people who are deaf, hard of hearing, or deafblind. But captions are also useful to anyone who is temporarily unable to perceive sound, for example inside a library.

On iOS, AVPlayer offers support to add captions. Users can automatically turn on subtitles via System Preferences.

The code example below shows a basic implementation of adding captions.

// 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
)

// Add captions track
guard let captionsUrl = Bundle.main.url(
    forResource: "Appt", 
    withExtension: ".vtt"
) else { 
    return 
}
guard let captionsTrack = videoComposition.addMutableTrack(
    withMediaType: .text, 
    preferredTrackID: kCMPersistentTrackID_Invalid
) else { 
    return 
}

let captionsAsset = AVURLAsset(url: captionsUrl)
try? captionsTrack.insertTimeRange(
    CMTimeRangeMake(start: .zero, duration: videoAsset.duration),
    of: subtitleAsset.tracks(withMediaType: .text)[0],
    at: .zero
)

Feedback?

Let us know!