jstuardo
jstuardo

Reputation: 4383

Video is not played in iOS project

I have used Plugin.MediaManager in my Xamarin.Forms application. It works perfectly in Android project, but in iOS project it does not.

I have added

VideoViewRenderer.Init();

in AppDelegate, and this is the code in the view:

    async void PlayStop_Clicked(object sender, System.EventArgs e)
    {
        if (this.BtnPlayStop.Text == "Start Video")
        {
            string video = Path.Combine(_videoPath, this.viewModel.Item.Video);

            if (File.Exists(video))
            {
                await CrossMediaManager.Current.Play(video, MediaFileType.Video);

                this.BtnPlayStop.Text = "Stop Video";
            }
        }
        else
        {
            await CrossMediaManager.Current.Stop();

            this.BtnPlayStop.Text = "Start Video";
        }
    }

Code enters the first if, since button changes its text to 'Stop Video' but no video appears. The video is a local mp4 file.

As I told, this works perfect in Android.

What's wrong?

Thanks

Jaime

Upvotes: 0

Views: 251

Answers (1)

jstuardo
jstuardo

Reputation: 4383

I have replaced the method that plays or stops the video by this one:

    async void PlayStop_Clicked(object sender, System.EventArgs e)
    {
        if (this.BtnPlayStop.Text == "Iniciar Video")
        {
            string video = Path.Combine(_videoPath, this.viewModel.Item.Video);

            if (File.Exists(video))
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    _ = CrossMediaManager.Current.Play("file://" + video, MediaFileType.Video);
                });

                this.BtnPlayStop.Text = "Detener Video";
            }
        }
        else
        {
            await CrossMediaManager.Current.Stop();

            this.BtnPlayStop.Text = "Iniciar Video";
        }
    }

The "file://" part is important when loading local media files.

With that method, it works in both Android and iOS.

Regards Jaime

Regards Jaime

Upvotes: 0

Related Questions