Forum Xamarin.Android

how to handle VideoView - OnCompletion, OnPrepared and OnError event in xamarin.android?

I am trying to play m3u8 video using videoview and mediacontroller, video is playing successful. but i dont have any idea how to handle onprepared for showing loading message and oncompletion for playing next video or showing list of video.Any idea how to handle it?

Thanks in advance!

Answers

  • StefanoTempestaStefanoTempesta USMember

    In the OnCreate method of you activity, get a reference to the videoView from the associated layout:

    VideoView videoView = FindViewById(Resource.Id.videoView);

    Then handle the OnXXX events by setting the respective listeners, for example:

    videoView.SetOnPreparedListener(new OnPreparedListener());

    A listener is a class that inherits from Java.Lang.Object and implement a specific listener interface, for example IOnPreparedListener for the OnPrepared event. Implement the methods of the interface accordingly.

    class OnPreparedListener : Java.Lang.Object, MediaPlayer.IOnPreparedListener
    {
    public void OnPrepared(MediaPlayer mp)
    {
    }
    }

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    @StefanoTempesta a succint approach would be to register for the VideoView's Prepared event once you obtain the handle to the VideoView. This is the manner .NET developers are accustomed to when registering events.

    Check out my post on VideoView playback that demonstrates this: http://geekswithblogs.net/lorilalonde/archive/2015/07/22/video-playback-in-your-xamarin.android-apps---part-2-adding.aspx

  • StefanoTempestaStefanoTempesta USMember

    Thanks Lori. How do I access an instance of MediaPlayer from the event handler?

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭
    edited July 2015

    You can either instantiate the MediaPlayer and associate it to the VideoView within the Prepared event, or if you plan on accessing MediaPlayer in other methods/events, you can declare it as a class level variable. In the blog post link I provided, I actually walk through the steps in declaring the MediaPlayer at the class level, but you can take the approach that works best for you.

  • CamelCaseMasterCamelCaseMaster USMember ✭✭
    edited December 2017

    2017 now & I find myself in this old post looking for an answer for the same question @StefanoTempesta

    VideoView.Prepared += (obj, args) => {
    
                    var MyMediaPlayer = obj as MediaPlayer;
    
                    MyMediaPlayer.Prepared += (Prepared_obj, Prepared_args) => {
                        //Insert Prepared Code Here
                    };
                    MyMediaPlayer.Completion += (Completion_obj, Completion_args) => {
                        //Insert Completion Code Here
                    };
                    MyMediaPlayer.Error += (Error_obj, Error_args) => {
                        //Insert Error Code Here
                    };
                };
    

    I hope I'm right, Merry Christmas!

  • Naveen9453Naveen9453 USMember ✭✭

    playVideo.Prepared += delegate{
    playVideo.start();
    };

    here playVideo is the reference of VideoView. why playVideo.Start() method never called. It seems that playVideo.Prepared event is not calling.
    Please Help.

  • Naveen9453Naveen9453 USMember ✭✭

    @LoriLalonde said:
    @StefanoTempesta a succint approach would be to register for the VideoView's Prepared event once you obtain the handle to the VideoView. This is the manner .NET developers are accustomed to when registering events.

    Check out my post on VideoView playback that demonstrates this: http://geekswithblogs.net/lorilalonde/archive/2015/07/22/video-playback-in-your-xamarin.android-apps---part-2-adding.aspx

    Its Shows This Blog is Not Active

Sign In or Register to comment.