Maui: [BUG] MediaElement Android video player throws exception

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the “Reporting a bug” section on Contributing file?

Current Behavior

in .net 9 maui, upgraded to the latest nuget packages as of today (27 Jan. 2025), MediaLElement version 6.0.0 When load the video player on Android (API 22), the following error threw:

Could not determine Java type corresponding to AndroidX.Media3.ExoPlayer.IExoPlayerInvoker, Xamarin.AndroidX.Media3.ExoPlayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. (Parameter ‘targetType’)

Expected Behavior

Video player worked fine prior to the nuget upgrades, video will play in the mediaElement. The new community toolkit nuget package broke the video player.

Steps To Reproduce

in. net 9 with all the latest nuget packages,

  1. Create a Xaml PAGE with the mediaElement instance.
  2. Set the mediaElement source video to a local video.
  3. When the page load, the exception throws

Link to public reproduction project repository

na

Environment

- .NET MAUI CommunityToolkit: latest nuget package as 27 Jan 2025
- .Net 9
- .on Android API 31

Anything else?

The problem only happens on Android verison.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 36 (14 by maintainers)

Most upvoted comments

I re-opened this issue for tracking purpose as @dhindrik and @dainius-r have also experiencing the same issue with the same repo. Even thoguh I don’t have to produce my own sample repo now.

I looked at the sample. I figured out an easy way to make the video work.

  private async Task HandleVideoResult(FileResult? fileResult)
  {
      if (fileResult is null)
      {
          return;
      }

      var tempFileName = $"temp_meps_video{Path.GetExtension(fileResult?.FileName)}";

      var stream = await fileResult?.OpenReadAsync();
      var memoryStream = new MemoryStream();
      await stream.CopyToAsync(memoryStream);

      var videosPath = FileSystem.AppDataDirectory;
      var localPath = Path.Combine(videosPath, tempFileName);
      File.WriteAllBytes(localPath, memoryStream.ToArray());

      stream.Close();
      memoryStream.Close();
      mediaElement.Source = MediaSource.FromFile(localPath);
  }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
	<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
		<service android:name="communityToolkit.maui.media.services" android:stopWithTask="true" android:exported="false" android:enabled="true" android:foregroundServiceType="mediaPlayback">
			<intent-filter>
				<action android:name="androidx.media3.session.MediaSessionService"/>
			</intent-filter>
		</service>
	</application>
	<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
	<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
	<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
	<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.CAMERA" />
	<!-- Required only if your app needs to access images or photos that other apps created -->
	<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
	<!-- Required only if your app needs to access videos that other apps created -->
	<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
	<!-- Required only if your app needs to access audio files that other apps created -->
	<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
</manifest>

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="AndroidMedia.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:View="clr-namespace:AndroidMedia"
    x:DataType="View:MainPage"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

    <Grid RowDefinitions="50,50 *">
        <Button Command="{Binding PickVideoCommand}" Text="Pick and open video" />
        <Button
            Grid.Row="1"
            Command="{Binding CaptureVideoCommand}"
            Text="Capture and open video" />
        <toolkit:MediaElement
            x:Name="mediaElement"
            Grid.Row="2"
            ShouldShowPlaybackControls="True" />
    </Grid>

</ContentPage>

Replace with above and it should work. It is not pretty but it will compile and run on android. No it will not work for every supported OS. But you can take it from here. If you have any more questions please open a discussion and ask there. I tested the revised version with android and API 34. Please move this to a discussion as this is not a bug.