Skip to content

VLCMediaPlayer: Multiple selection of media tracks

Hank Anderson requested to merge Sumou/VLCKit:multi-track-selection into master

Proposal for multiple selection of media tracks.

// example
let mediaPlayer: VLCMediaPlayer = ...

// Get tracks
// Audio Tracks
let audioTracks: [VLCMediaPlayer.Track] = mediaPlayer.audioTracks
// Video Tracks
let videoTracks: [VLCMediaPlayer.Track] = mediaPlayer.videoTracks
// Text Tracks
let textTracks: [VLCMediaPlayer.Track] = mediaPlayer.textTracks

// Subtitle selection
guard let sub = textTracks.first else { return }
sub.isSelected = true

// Deselect subtitles
sub.isSelected = false

// Deselect all subtitles
mediaPlayer.deselectAllTextTracks()

A little practical example

// When using UITableView
let mediaPlayer: VLCMediaPlayer
var audioTracks: [VLCMediaPlayer.Track] = []
var videoTracks: [VLCMediaPlayer.Track] = []
var textTracks: [VLCMediaPlayer.Track] = []

func loadData() {
    audioTracks = mediaPlayer.audioTracks
    videoTracks = mediaPlayer.videoTracks
    textTracks = mediaPlayer.textTracks
    tableView.reloadData()
}

func getTracks(section: Int) -> [VLCMediaPlayer.Track] {
    switch section {
        case 0:     return audioTracks
        case 1:     return videoTracks
        case 2:     return textTracks
        default:    fatalError()
    }
}

// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
    let tracks: [VLCMediaPlayer.Track] = getTracks(section: indexPath.section)
    if tracks.count < indexPath.row {
        let track = tracks[indexPath.row]
        cell.textLabel?.text = track.trackName
        cell.accessoryType = track.isSelected ? .checkmark : .none
    }
    return cell
}

// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let tracks: [VLCMediaPlayer.Track] = getTracks(section: indexPath.section)
    guard tracks.count < indexPath.row else { return }
        
    let track = tracks[indexPath.row]
    track.isSelected.toggle()

    // reload UI
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
        tableView.reloadData()
    }
}

Related #586 (closed)

If this attempt is not suitable, please close this merge request.

best regards.

Edited by Felix Paul Kühne

Merge request reports

Loading