Search code examples
windows-phone-8media-playerisolatedstoragesubtitlewebvtt

How to open Media Player WebVTT caption from isolated storage


I have mp4+vtt subtitle video player project on Wp8 c#. I'm look at Microsoft.PlayerFramework.MediaPlayer and WebVTTPlugin, its works perfect: https://playerframework.codeplex.com/wikipage?title=Closed%20Captions%3a%20WebVTT im use this code and works perfect. But i have bad luck my project .vtt caption files have "," decimal seperator, this mean my app chrash i need to download subtitle and replace all "," to "." and save isolated storage, i handle it but i cant set caption source to isolated storage because isolated storage is not have uri. I know i cant tell well, i tell it with example:

my caption: (http://dizilab.com/captions/chuck/sezon-1/tr/2.vtt?v=5.2)

1 00:00:06,600 --> 00:00:10,900 Merhaba. Benim adım Charles Bartowski, ama bana Chuck diyebilirsiniz.

2 00:00:11,323 --> 00:00:12,711 Bunlar benim ayakkabılarım.

3 00:00:12,771 --> 00:00:14,209 Bu da benim hayatım.

4 00:00:14,249 --> 00:00:19,042 Casuslar, araba takipleri, bilgisayar çalan ninjalar ve günü kurtaran ben.

its true caption is:

WEBVTT FILE 1 00:00:06.600 --> 00:00:10.900 Merhaba. Benim adım Charles Bartowski, ama bana Chuck diyebilirsiniz.

2 00:00:11.323 --> 00:00:12.711 Bunlar benim ayakkabılarım.

3 00:00:12.771 --> 00:00:14.209 Bu da benim hayatım.

4 00:00:14.249 --> 00:00:19.042 Casuslar, araba takipleri, bilgisayar çalan ninjalar ve günü kurtaran ben.

and it is code:

using Microsoft.PlayerFramework.WebVTT;
using System.IO.IsolatedStorage;
using System.IO;
using System.Threading.Tasks;
using Microsoft.PlayerFramework;

namespace PanoramaApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public string alinanveri="";
        public MainPage()
        {
            InitializeComponent();              

            Microsoft.PlayerFramework.MediaPlayer player =new Microsoft.PlayerFramework.MediaPlayer(); 
            Microsoft.PlayerFramework.WebVTT.WebVTTPlugin webvttPlugin = new WebVTTPlugin();
            Microsoft.PlayerFramework.Caption caption = new Microsoft.PlayerFramework.Caption(); 
            player.IsCaptionSelectionVisible = true;  
            player.Plugins.Add(webvttPlugin);
            altyazikaydet("http://dizilab.com/captions/chuck/sezon-1/tr/2.vtt?v=5.2");
            IsolatedStorageFile kayitliDepo = IsolatedStorageFile.GetUserStoreForApplication();

            var okuyucu = new StreamReader(new IsolatedStorageFileStream("altyazi.vtt", FileMode.Open, kayitliDepo));
            caption.Source = new Uri("i cant use here for access isostorage"); // url points to sample.vtt file

            caption.Description = "Türkçe";            
            player.AvailableCaptions.Add(caption);
            player.SelectedCaption = player.AvailableCaptions.FirstOrDefault();
            LayoutRoot.Children.Add(player);

             player.Source = new Uri("https://redirector.googlevideo.com/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=eafe5f42d368b2e0&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1420976098&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=6E257266C2AAADDFC3260B0AADE603F7E421E130.A933FF8365247DEC72A34B71B02FA3B13C57F291&key=lh1", UriKind.RelativeOrAbsolute); // url points to sample.mp4 fil
        }

        private  async void altyazikaydet(string altyaziurl)
        {
            try
            {

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(altyaziurl);


            using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
            {
                using (var responseStream = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(responseStream))
                    {

                        alinanveri = await sr.ReadToEndAsync();
                    }
                }
            }
                IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                StreamWriter yazici = new StreamWriter(new IsolatedStorageFileStream("altyazi.vtt", FileMode.Create, file));

                string x=alinanveri;
                x=x.Replace(",0",".0");
                x=x.Replace(",1",".1");
                x=x.Replace(",2",".2");
                x=x.Replace(",3",".3");
                x=x.Replace(",4",".4");
                x=x.Replace(",5",".5");
                x=x.Replace(",6",".6");
                x=x.Replace(",7",".7");
                x=x.Replace(",8",".8");
                x=x.Replace(",9",".9");
                x = "WEBVTT FILE" + Environment.NewLine + x;
                yazici.WriteLine(x);                
                yazici.Close();
                IsolatedStorageFile kayitliDepo = IsolatedStorageFile.GetUserStoreForApplication();

                StreamReader okuyucu = new StreamReader(new IsolatedStorageFileStream("altyazi.vtt", FileMode.Open, kayitliDepo));

                    string line;
                    while ((line = okuyucu.ReadLine()) != null)
                    {
                        MessageBox.Show(line);
                    }



            }
            catch
            {

            }
        }

Solution

  • I had the same problem in Windows UWP. The player.Source allows an absolute local URI but the webvtt plugin does not.

    Finally, I found out how to make it work:

    using System.IO;
    
    var caption = new Caption
    {
        Description = "whatever",
        Source = new Uri(Path.Combine("ms-appdata:///Local/", "test.vtt"))
    };
    
    Player.AvailableCaptions.Add(caption);
    Player.SelectedCaption = Player.AvailableCaptions.FirstOrDefault();