How to specify encoding of subtitles for TimedTextSource when using MediaPlayerElement in UWP

Jan Drozd 111 Reputation points
2021-05-26T13:45:07.327+00:00

Hi once more,

we've found an issue with TimedTextSource which affects our app and also built-in Movies & TV app.

This one generates bad characters during playback:
99877-a23b9b3a-d983-4e68-87f4-b30c63adb35eorigsrt.txt

This is the same file, but quote („) character was replaced by ASCII quote ("). These subtitles works well.
99915-a23b9b3a-d983-4e68-87f4-b30c63adb35esrt.txt

Both files are UTF8 encoded. So my question is, is there a way I can force the MediaPlayerElement to use UTF8 encoding instead of autodetection? Or do you see any other how to make first file working without bad characters?

Developer technologies | Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2021-05-27T03:19:57.157+00:00

    Hello, Welcome to Micorosoft Q&A,

    So my question is, is there a way I can force the MediaPlayerElement to use UTF8 encoding instead of autodetection?

    Currently, there is not such api to force the MediaPlayerElement to use UTF8 encoding for TimedTextSource, the better way is convert the string as Utf8, then pass it to TimedTextSource

    For example

    FileOpenPicker openPicker = new FileOpenPicker();  
    openPicker.ViewMode = PickerViewMode.Thumbnail;  
    openPicker.SuggestedStartLocation = PickerLocationId.Desktop;  
    openPicker.FileTypeFilter.Add(".txt");  
    StorageFile file = await openPicker.PickSingleFileAsync();  
    if (file != null)  
    {  
        IBuffer buffer = await FileIO.ReadBufferAsync(file);  
        using (var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(buffer))  
        {  
            string text = dataReader.ReadString(buffer.Length);  
            var newbuffer = CryptographicBuffer.ConvertStringToBinary(text, BinaryStringEncoding.Utf8);  
            using (var memstream = newbuffer.AsStream())  
            {  
                TimedTextSource source = TimedTextSource.CreateFromStream(memstream.AsRandomAccessStream());  
            }  
        }     
    }  
    

    Or do you see any other how to make first file working without bad characters?

    Sure, you could filter the specific and replace it with you want one.

     var temp = text.Replace('„', '"').Replace('“', '"');  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.