Android Videoview add subtitle

JessaJessa TRMember ✭✭
edited April 2017 in Xamarin.Android

My code is this.

    private InputStream getSubtitleSource(string filepath)
    {
        InputStream ins = null;
        string ccFileName = filepath.Substring(0, filepath.LastIndexOf('.')) + ".srt";
        Java.IO.File file = new Java.IO.File(ccFileName);
        if (file.Exists() == false)
        {
            return null;
        }
        FileInputStream fins = null;
        try
        {
            fins = new FileInputStream(file);
        }
        catch (Exception e)
        {
        }
        ins = (InputStream)fins;
        return ins;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<View_Video> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null || Element == null)
            return;

        videoView = new Android.Widget.VideoView(Forms.Context);

        mediaController = new MediaController(Forms.Context);
        mediaController.SetMediaPlayer(videoView);
        videoView.SetMediaController(mediaController);
        videoView.AddSubtitleSource(getSubtitleSource(Element.SubTitleUrl), MediaFormat.CreateSubtitleFormat("text/srt", "tr"));
        SetNativeControl(videoView);

        videoView.SetVideoURI(Android.Net.Uri.Parse(Element.Url));
        progress = new ProgressDialog(Forms.Context, Resource.Style.progress_bar_style);
        progress.Indeterminate = true;
        progress.SetProgressStyle(ProgressDialogStyle.Spinner);
        progress.SetCancelable(false);
        progress.Show();
        progress.SetContentView(new Android.Widget.ProgressBar(Forms.Context));

        videoView.SetOnPreparedListener(this);
    }

I get this error on the line I wrote below (Cannot convert from 'Java.IO.InputStream' to 'System.IO.Stream')
videoView.AddSubtitleSource(getSubtitleSource(Element.SubTitleUrl), MediaFormat.CreateSubtitleFormat("text/srt", "tr"));

Answers

  • Sushil_KumarSushil_Kumar Member ✭✭

    @Jessa said:
    My code is this.

        private InputStream getSubtitleSource(string filepath)
        {
            InputStream ins = null;
            string ccFileName = filepath.Substring(0, filepath.LastIndexOf('.')) + ".srt";
            Java.IO.File file = new Java.IO.File(ccFileName);
            if (file.Exists() == false)
            {
                return null;
            }
            FileInputStream fins = null;
            try
            {
                fins = new FileInputStream(file);
            }
            catch (Exception e)
            {
            }
            ins = (InputStream)fins;
            return ins;
        }
    
        protected override void OnElementChanged(ElementChangedEventArgs<View_Video> e)
        {
            base.OnElementChanged(e);
    
            if (e.OldElement != null || Element == null)
                return;
    
            videoView = new Android.Widget.VideoView(Forms.Context);
    
            mediaController = new MediaController(Forms.Context);
            mediaController.SetMediaPlayer(videoView);
            videoView.SetMediaController(mediaController);
            videoView.AddSubtitleSource(getSubtitleSource(Element.SubTitleUrl), MediaFormat.CreateSubtitleFormat("text/srt", "tr"));
            SetNativeControl(videoView);
    
            videoView.SetVideoURI(Android.Net.Uri.Parse(Element.Url));
            progress = new ProgressDialog(Forms.Context, Resource.Style.progress_bar_style);
            progress.Indeterminate = true;
            progress.SetProgressStyle(ProgressDialogStyle.Spinner);
            progress.SetCancelable(false);
            progress.Show();
            progress.SetContentView(new Android.Widget.ProgressBar(Forms.Context));
    
            videoView.SetOnPreparedListener(this);
        }
    

    I get this error on the line I wrote below (Cannot convert from 'Java.IO.InputStream' to 'System.IO.Stream')
    videoView.AddSubtitleSource(getSubtitleSource(Element.SubTitleUrl), MediaFormat.CreateSubtitleFormat("text/srt", "tr"));

    Jessa Have you got solution of this code. If got solution than please help me, I also want to set Subtitle on video (Caption/Subtitle).

    Thanks.

Sign In or Register to comment.