Search code examples
c#vlclibvlclibvlcsharp

How do you turn subtitles on and off while the video is playing in LibVLCSharp?


How do you turn subtitles on and off while the video is playing in LibVLCSharp?

I can set it when I initialize the media player. Once the media player is playing the video, how can I toggle the subtitles on and off during playback?

I've tried options here but can't get it to work.

https://wiki.videolan.org/VLC_command-line_help/

Thanks in advance

UPDATE:

I got the subtitles to turn off and on before play with

public void PlayMedia(string mediaFileName)
    {
        _media = new Media(_libVLC, new Uri(mediaFileName));
        _media.AddOption(string.Format(@":sub-track-id={0}", int.MaxValue)); //turn off
        _mp.Play(_media);
    }

While playing I tried to do something like

_media.AddOption(string.Format(@":sub-track-id={0}", 2)); //2 was value in SpuDescription in debugger to enable subtitles

Referenced: https://github.com/Stremio/react-native-vlc2/pull/4/commits/e5646acab6423ceb328c31eaeac3200d6ba0b91f

This does not work while the media player is playing

How do I changed this value while media is playing?

UPDATE 2

I found an UGLY workaround (see below) but there has to be a cleaner way of doing this....

var time = _mp.Time;
_mp.Stop();
_media.AddOption(string.Format(@":sub-track-id={0}", 2));
_mp.Play();
_mp.Time = time;

Any ideas on how to clean this up and toggle subtitles on and off during playback?


Solution

  • _mp.SetSpu(-1);
    

    SPU (Sub Picture Unit) means essentially subtitles in VLC jargon.

    You can get more info about subtitles tracks with the following property which returns an array of the spu tracks:

    _mp.SpuDescription
    

    The first one should be the "disable" option and have a -1 ID.