question

JanDrozd-9035 avatar image
0 Votes"
JanDrozd-9035 asked NicoZhu-MSFT commented

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

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?


windows-uwp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How do you create TimedTextSource, create from url or stream?

0 Votes 0 ·

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered NicoZhu-MSFT commented


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.





· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have tried the code which is converting the StorageFile to buffer, then to string and then back to memory stream. Below is my code I used. It didn't help, unfortunately.


         public static async Task<IRandomAccessStream> GetSubtitlesStreamUtf8(StorageFile file)
         {
             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);
                     var memstream = newbuffer.AsStream();
    
                     return memstream.AsRandomAccessStream();
    
                 }
             }
    
    
             return null;
         }
0 Votes 0 ·

When the subtitle file is saved using UTF-8 with BOM it helps too and characters are displayed correctly in MediaPlayerElement.

0 Votes 0 ·

Above code could convert string to utf8, but it could not covert specific char such as ('„' '"'), please use Replace method to convert specific char.

1 Vote 1 ·