Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Using MediaElement in Android causes crash #2419

Open
2 tasks done
mr5z opened this issue Dec 28, 2024 · 0 comments · May be fixed by #2418
Open
2 tasks done

[BUG] Using MediaElement in Android causes crash #2419

mr5z opened this issue Dec 28, 2024 · 0 comments · May be fixed by #2418
Labels
bug Something isn't working unverified

Comments

@mr5z
Copy link

mr5z commented Dec 28, 2024

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

The following XAML code produces a crash on my Android Tablet device:

<toolkit:MediaElement
    x:Name="MediaElement"
    Grid.Row="1"
    ShouldAutoPlay="False"
    ShouldKeepScreenOn="True"
    ShouldShowPlaybackControls="True"
    Aspect="AspectFill"
    MetadataTitle="{Binding VideoTitle}"
    Source="{Binding VideoSource}" />

This happens on my case when the previous page has another running Android Service, but I think it has nothing to do with it, rather, the Android Notification in MediaControlsService setup seems to be modifying the Action object behavior even before the Notification object has been configured.

There is a similar issue in StackOverflow, that seems to back up my claim on this and the solution was to add the Actions first before configuring it.

Expected Behavior

When another Android Service is running, the MediaControlsService shouldn't crash the app. A simple code example like the code below should also be expected to run without issues:

Page.xaml

<toolkit:MediaElement
    x:Name="MediaElement"
    Grid.Row="1"
    ShouldAutoPlay="False"
    ShouldKeepScreenOn="True"
    ShouldShowPlaybackControls="True"
    Aspect="AspectFill"
    MetadataTitle="{Binding VideoTitle}"
    Source="{Binding VideoSource}" />

MauiProgram.cs

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            .UseMauiCompatibility()
            .UseScreenRecording()                     // The other Android Service
            .UseMauiCommunityToolkitMediaElement();   // MediaControlsService
        // ...
    }
}

AndroidManifest.xml

    <!-- other things -->

    <!-- Video Player -->
    <service android:name="communityToolkit.maui.media.services" android:exported="false" android:enabled="true" android:foregroundServiceType="mediaPlayback">
      <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
      </intent-filter>
      <intent-filter>
        <action android:name="androidx.media3.session.MediaSessionService"/>
      </intent-filter>
    </service>
    
  </application>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
  <!-- This one is only needed when targeting API 34 and up -->
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />

Steps To Reproduce

  1. Install ScreenRecording
  2. Install MediaElement
  3. Create a page that starts ScreenRecording and stops it as soon as it navigates to next page
  4. Next page would be the one hosting MediaElement
  5. Play a video file from file system (file system might not be necessary here)
  6. As soon as it transitions to the page with MediaElement, it would crash

Link to public reproduction project repository

https://github.com/mr5z/Repro-Maui.MediaElement.ScreenRecording

Environment

- .NET MAUI CommunityToolkit: 10.0.0
- OS: Android 13
- .NET MAUI: 9.0.21

Anything else?

@mr5z mr5z added bug Something isn't working unverified labels Dec 28, 2024
@mr5z mr5z linked a pull request Dec 28, 2024 that will close this issue
6 tasks
@mr5z mr5z changed the title [BUG] [BUG] Using MediaElement in Android causes crash Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unverified
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant