Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Android Player - Can't play this video

I'm using Android's VideoView to play an embedded video in my app. It works fine on my device but I keep getting a "Can't play this video" message and a black screen in the Xamarin Android Player.

error message

The corresponding error log looks like this:

Unable to play video
[MediaPlayer] Error (1,-38)
[VideoView] Error: 1,-38

I found a few posts regarding this error but none of them helped me solving this issue and I'm not able to find a proper description for this status code.

My C# code looks like this:

videoView = new VideoView (Context);
base.SetNativeControl (videoView);
videoView.SetOnErrorListener (new ErrorListener ());

string fileName = e.NewElement.FileSource;
fileName = fileName.ToLower ().Substring (0, fileName.LastIndexOf ("."));
int resourceID = Context.Resources.GetIdentifier (fileName, "raw", Context.PackageName);
var fullPath = String.Format ("android.resource://{0}/{1}", Context.PackageName, resourceID);

videoView.SetVideoPath (fullPath);
videoView.RequestFocus ();
videoView.Start ();
like image 962
Lars Avatar asked Jul 20 '15 11:07

Lars


1 Answers

Seems to be an issue with the type of encoding that the emulator supports, if you install ffmpeg, if your on a mac by running these commands:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew install ffmpeg 

then process your video file with:

ffmpeg -i big_buck_bunny_720p_1mb.mp4 -c:v libx264 -profile:v baseline -c:a aac -strict -2 -b:a 128k output.mp4 

and try to play the output it wont show that error but it will be a blank video (just a black screen). So I think the issue is just getting the right encoding, have tried some different encodings but all seems to just show a black screen.

Will do some more digging but for the time being it seems to be just the emulator does not support your encoding.

EDIT

Ok so I got the videoplay working, I processed the video with:

ffmpeg -i SampleVideo_1080x720_1mb.mp4 -codec:v libx264 -profile:v baseline -preset slow -b:v 250k -maxrate 250k -bufsize 500k -vf scale=-1:360 -threads 0 -codec:a aac -strict -2 -b:a 96k output.mp4

Check this site for the ffmpeg parameters. I setup my VideoView like so:

public class Activity1 : Activity
    {
        VideoView videoView;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            videoView = FindViewById<VideoView> (Resource.Id.SampleVideoView);
            videoView.SetMediaController(new MediaController(this));
            videoView.SetVideoPath ($"android.resource://{PackageName}/{Resource.Raw.output}");
            videoView.RequestFocus ();
            videoView.Start ();
        }
    }

This seems to work on the Xamarin Android Player but only for API versions 16(JellyBean) and 19(Kitkat). 21 (lollipop) just doesn't load the video.

Then I downloaded the GenyMotion Emulator (need to create an account but its free for personal use) to check if it was the Xamarin Player or not. It works on all (16,17,18,19,20 + 22) apart from 21(lollipop). looks like something is wrong with the emulators for 21, I did all my testing on nexus 4 emulators. So if you want tot est video playback I would try to avoid emulators with API 21.

like image 84
Iain Smith Avatar answered Sep 16 '22 14:09

Iain Smith