Video is not played in iOS project

jstuardojstuardo ✭✭Member ✭✭

Hello,

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

Answers

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    If you want to display a video on the screen, I think you need to use VideoView:

    <StackLayout>
        <mm:VideoView VerticalOptions="FillAndExpand" Source="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ShowControls="False" />
    </StackLayout>
    

    And I didn't find Play could access the MediaFileType how did you make it work on Android?

  • jstuardojstuardo ✭✭ Member ✭✭

    I am using VideoView, but the source is not set at design time, but at run time by using CrossMediaManager.Current.Play.

    However, it worked finally.

    What I did was:

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

    The use of "file://" for a local file is important.

    Regards
    Jaime

  • jstuardojstuardo ✭✭ Member ✭✭

    I have one more issue though:

    In view constructor, I have this code:

                string video = Path.Combine(_videoPath, this.viewModel.Item.Video);
    
                if (File.Exists(video))
                {
                    MyVideo.Source = "file://" + video;
                }
    

    That is there to start playing the video just when the page is shown. This works in iOS but not in Android. In Android, I have to press the button, which calls PlayStop_Clicked, to start playing the video. In iOS, video starts immediately.

    What is wrong?

    Regards
    Jaime

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    Maybe platform related. Add a play function below your source setting code.

  • jstuardojstuardo ✭✭ Member ✭✭
    This is the shared project. Since this works in iOS but not in Android, that play function should be called only when platform is Android. How can I check this? By mean of a run-time test or a preprocessor directive? How is this kind of task normally done?
  • TaylorDTaylorD ✭✭✭ USMember ✭✭✭

    Xamarin.Forms gives you access to the platform you are currently running on. Use the code below to check if you are running on Android.

    if (Device.RuntimePlatform == Device.Android)
    {
        // Play video
    }
    
Sign In or Register to comment.