0

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

jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • Have you tried wrapping this line "await CrossMediaManager.Current.Play(video" inside an invokeonmainthread. The event is called on a separate thread that causes the video to play but not on the main UI thread – Saamer Jul 14 '19 at 02:44
  • That code is in the shared project. Will that work in Android too? – jstuardo Jul 14 '19 at 03:14
  • Review the device|simulator log for AVKit warning/errors produced by the AVPlayer. – SushiHangover Jul 14 '19 at 04:18
  • @SushiHangover it is not the log I already posted? What is that log and how to get it? I am with Visual Studio 2019 for Mac. – jstuardo Jul 14 '19 at 13:25
  • Yes it should work on both, did it work? Device.BeginInvokeOnMainThread(() => { CrossMediaManager.Current.Play(video, MediaFileType.Video);}); – Saamer Jul 14 '19 at 17:36
  • @jstuardo You have not posted any log... https://devblogs.microsoft.com/xamarin/enhanced-device-logging-in-visual-studio/ – SushiHangover Jul 14 '19 at 18:23
  • @Saamer it did not work. No video started playing. – jstuardo Jul 14 '19 at 18:51
  • 1
    @SushiHangover this is shown in the log: Error (351) / MyApp.iOS(CFNetwork): NSURLConnection finished with error - code -1002. I am not sure that call is generating that error. – jstuardo Jul 14 '19 at 18:57
  • 1
    I have added file:// before the actual video path, and the problem was solved. – jstuardo Jul 14 '19 at 20:23
  • @jstuardo Hi , if have solved , remember to share slution in answer. :) – Junior Jiang Jul 15 '19 at 05:42

1 Answers1

0

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

jstuardo
  • 3,901
  • 14
  • 61
  • 136