Ga naar hoofdinhoud
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.

Op iOS kun je ondertiteling toevoegen via AVPlayer. Gebruikers kunnen ondertiteling automatisch aan laten zetten via de systeeminstellingen.

Het codevoorbeeld toont een versimpelde manier om ondertiteling toe te voegen.

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

Feedback?

Laat 't ons weten!