简体   繁体   中英

Xamarin.Forms Image gif animation doesn't play on android

I'am new to Xamarin and I try to play a Gif in a Image control. In my iOS Emulator it works very well but when I start the app in my Android emulator, the gif doesn't play. I don't use Legacy Renderer but I never specified using fast renderer (but I don't have to I think). Thats my XAML File:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Connect.Views.SplashPage">
<ContentPage.Content>
    <StackLayout Margin="20,35,20,20">
        <Image Source="splashAni.gif" VerticalOptions="CenterAndExpand"
               HorizontalOptions="Center" IsAnimationPlaying="True"/>
    </StackLayout>
</ContentPage.Content>

I don't know what's wrong, I hope you can help me, thanks in advance!

A bit late to the game here but here is a working example by Gerald Versluis that I was able to use to test some scenarios. It's a basic example that links out to source code on GitHub.

I'm suspicious that your resource might not be included in the Android project and it's not able to find the file. In Gerald's example his animated gif is located in both AnimatedGifForms.Android\Resources\drawable and AnimatedGifForms.iOS\Resources respectively.

Though you do mention it works for you in iOS. It may be worth your time to look at his simple example and compare it to what you're doing.

On that note:

I've tried using a modified version of his example to load an animated gif that is an EmbeddedResource in the NetStandard shared library of the project but have been unsuccessful. I'm using the ImageSource.FromStream( () => assembly.GetManifestResourceStream( resourcePath ) ); method to create the ImageSource from a stream in a binding converter. I can load static gif images without problems this way. I can even load the animated image, as a static image without issue (the IsAnimationPlaying property must be absent from the xaml), but when I add/set the IsAnimationPlaying property in xaml the Android app crashes with the following:

=================================================================
    Native Crash Reporting
=================================================================
Got a SEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

No native Android stacktrace (see debuggerd output).

=================================================================
    Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x8c83a896):
0x8c83a886  10 8d 4d e4 8b 4d 10 89 4d e4 8d 4d e4 8b 4d e4  ..M..M..M..M..M.
0x8c83a896  89 4c 24 08 89 44 24 04 89 3c 24 8b c0 e8 c0 fe  .L$..D$..<$.....
0x8c83a8a6  ff ff 89 45 c4 8b 4d f0 8d 64 24 00 90 90 90 8b  ...E..M..d$.....
0x8c83a8b6  45 c4 e9 77 00 00 00 8b 4d f0 8d 64 24 00 90 90  E..w....M..d$...

=================================================================
    Managed Stacktrace:
=================================================================
      at Xamarin.Forms.Platform.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0012e>
      at Xamarin.Forms.Platform.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0013f>
      at Xamarin.Forms.Platform.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0013f>
      at Xamarin.Forms.Platform.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0013f>
      at Xamarin.Forms.Platform.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0013f>
      at Xamarin.Forms.Platform.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0
.....
      at Xamarin.Forms.Platform.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0013f>
      at Xamarin.Forms.Platform.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0013f>
      at Xamarin.Forms.Platform
.Android.FormsAnimationDrawable:LoadImageAnimationAsync <0x0013f>
      at Xamarin.Forms.Platform.Android.StreamImagesourceHandler:LoadImageAnimationAsync <0x0005b>
      at <UpdateBitmap>d__2:MoveNext <0x00768>
      at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start <0x00127>
      at Xamarin.Forms.Platform.Android.ImageViewExtensions:UpdateBitmap <0x001bf>
      at Xamarin.Forms.Platform.Android.ImageViewExtensions:UpdateBitmap <0x00067>
      at <TryUpdateBitmap>d__6:MoveNext <0x0028f>
      at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start <0x00147>
      at Xamarin.Forms.Platform.Android.FastRenderers.ImageElementManager:TryUpdateBitmap <0x0019b>
      at <OnElementChanged>d__3:MoveNext <0x004b7>
      at System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start <0x00127>
      at Xamarin.Forms.Platform.Android.FastRenderers.ImageElementManager:OnElementChanged <0x0015f>
      at System.EventHandler`1:invoke_void_object_TEventArgs <0x00143>
      at Xamarin.Forms.Platform.Android.FastRenderers.ImageRenderer:OnEl
ementChanged <0x0011b>
      at Xamarin.Forms.Platform.Android.FastRenderers.ImageRenderer:Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement <0x005d7>
      at Xamarin.Forms.Platform.Android.Platform:CreateRenderer <0x0013f>
      at Xamarin.Forms.Platform.Android.VisualElementPackager:AddChild <0x00487>
      at Xamarin.Forms.Platform.Android.VisualElementPackager:SetElement <0x00837>
      at Xamarin.Forms.Platform.Android.VisualElementPackager:Load <0x00057>
      at Xamarin.Forms.Platform.Android.VisualElementRenderer`1:SetPackager <0x00077>
      at Xamarin.Forms.Platform.Android.VisualElementRenderer`1:SetElement <0x007b7>
      at Xamarin.Forms.Platform.Android.VisualElementRenderer`1:Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement <0x00187>
      at Xamarin.Forms.Platform.Android.Platform:CreateRenderer <0x0013f>
      at Xamarin.Forms.Platform.Android.VisualElementPackager:AddChild <0x00487>
      at Xamarin.Forms.Platform.Android.VisualElementPackager:SetElement <0x00837>
      at Xamarin.Forms.Platform
.Android.VisualElementPackager:Load <0x00057>
      at Xamarin.Forms.Platform.Android.VisualElementRenderer`1:SetPackager <0x00077>
      at Xamarin.Forms.Platform.Android.VisualElementRenderer`1:SetElement <0x007b7>
      at Xamarin.Forms.Platform.Android.VisualElementRenderer`1:Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement <0x00187>
      at Xamarin.Forms.Platform.Android.Platform:CreateRenderer <0x0013f>
      at Xamarin.Forms.Platform.Android.AppCompat.Platform:AddChild <0x000b3>
      at Xamarin.Forms.Platform.Android.AppCompat.Platform:SetPageInternal <0x00297>
      at Xamarin.Forms.Platform.Android.AppCompat.Platform:SetPage <0x0067f>
      at Xamarin.Forms.Platform.Android.FormsAppCompatActivity:InternalSetPage <0x001b7>
      at Xamarin.Forms.Platform.Android.FormsAppCompatActivity:SetMainPage <0x00077>
      at Xamarin.Forms.Platform.Android.FormsAppCompatActivity:LoadApplication <0x0067b>
      at AnimatedGifForms.Droid.MainActivity:OnCreate <0x00143>
      at Android.App.Activity:n_OnCreate_Landroid_os_Bundle_ <0
x000ab>
      at Android.Runtime.DynamicMethodNameCounter:8 <0x000c3>
      at Android.Runtime.DynamicMethodNameCounter:8 <0x000b3>
=================================================================
[HotReload] (2020-05-29 10:24:00.4): ERROR: Caught exception in AgentStatusChangedHandler at 212: Xamarin.HotReload.DebuggerTimeoutException: Failed to Inject Assembly
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

Which continues ad nauseam for pages and then it just dies. I've upgraded to the latest version of Xamarin.Forms (4.6.0.772) and Essentials (1.5.3.2) with no change in outcome.

I've spent all day on this. As far as I can tell, most gifs do not actually work in Xamarin Forms. They just show up as single frame static images, when they don't crash first. Even when it does work (which only seems to be in trivially simple apps, like the example app) , it takes 5+ seconds to load the image.

The feature is just completely broken and should be avoided.

The alternative is...

Convert yo gif's to mp4 and use MediaElement from XCT : https://github.com/xamarin/XamarinCommunityToolkit

Like so:

<xct:MediaElement WidthRequest="310"
                              HeightRequest="345"
                              BackgroundColor="Transparent"
                              AutoPlay="True"
                              ShowsPlaybackControls="False"
                              HorizontalOptions="Center"
                              IsLooping="True"
                              Source="ms-appx:///house.mp4" />

Read more about Source here: https://docs.microsoft.com/en-us/xamarin/community-toolkit/views/mediaelement#play-media-embedded-in-the-app-package

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM