Baseflow / XamarinMediaManager

Cross platform Xamarin plugin to play and control Audio and Video
https://baseflow.com
MIT License
761 stars 304 forks source link

Version 1.1.1 does not create player in Notification Manager on Android 12 #877

Closed copang closed 1 year ago

copang commented 1 year ago

🔙 Regression

I'm using version 1.1.1 and it can work with Android 12, however I noticed that it doesn't create player in notification manager like previous versions (1.0.9), this results in audio only being playable in background about 15-30m and then will be stopped. Devices running Android lower than 12 still work properly. This issue only occurs on devices running Android 12.

Old (and correct) behavior

  1. Play audio
  2. Go to background
  3. There are media player in Notification Manager -> can play audio all night

Current behavior

  1. Play audio
  2. Go to background
  3. No media player in Notification Manager -> audio stop after 15-30m

Configuration

Xamarin.Forms 5.0.0.2515 Plugin.MediaManager.Forms 1.1.1

Version: 1.1.1

Platform:

arahmancsd commented 1 year ago

same here, updated to Android 12. Got the player playing the media, but I can't see anything in the notification area. Also, Support Android target level api 31 #866 doesn't mention anything at the end regarding the issue.

ArchangelWTF commented 1 year ago

It's probably due to lie in the MediaBrowserService not creating the notification correctly due to Google having removed parts of code there that initialize the NotificationListener which in term starts the notification.

copang commented 1 year ago

Thanks @ArchangelWTF for fixing it, but with current version (using .net 6) how can I use it for Xamarin Forms? If yes hope you give me some tutorial. Thank you very much!

ArchangelWTF commented 1 year ago

If you need backwards compatibility for now for a Xamarin Forms app your best bet would be to create a Fork branched off of this commit https://github.com/Baseflow/XamarinMediaManager/commit/24cf94f9a66a7fe6b3daf73198cb93fbe77745e8 and manually do the change my PR did to the MediaBrowserService and then compile it for yourself to use in XF

arahmancsd commented 1 year ago

@ArchangelWTF I forked the recent changes and packed the required libs with your changes. It is still not working in Android 12.

janwiebe-jump commented 1 year ago

@arahmancsd I did more or less the same at https://github.com/janwiebe-jump/XamarinMediaManager/tree/xamarin-android-12 It does create the notification on playing on my Android 13 phone.

copang commented 1 year ago

@ArchangelWTF, I tried your suggestion and found that the PlayerNotificationManager.Builder method needed to pass 4 parameters, so I fixed it as follows: PlayerNotificationManager = new Com.Google.Android.Exoplayer2.UI.PlayerNotificationManager.Builder( this, ForegroundNotificationId, ChannelId, MediaDescriptionAdapter) .SetNotificationListener(NotificationListener) .Build(); In addition, I had some other errors, but when I removed the targets xamarin.mac20, xamarin.tvos10, tizen40, I was able to build the package for Forms. But it still can't create player in notification manager on Android 12 devices. Am I doing something wrong?

copang commented 1 year ago

@janwiebe-jump, I tried using your version and to be able to build I also had to remove some targets like in the above comment (I don't know if it caused any errors). However, it still doesn't create player in notification manager, I tested it on Oneplus and Xiaomi phones running Android 12. If you have successfully tested it, can you send me a copy of the package you used? Thanks!

janwiebe-jump commented 1 year ago

hmm, my version doesn't work either unfortunately. Playing doesn't start.

copang commented 1 year ago

@janwiebe-jump you can try workaround from #876

sschaub commented 1 year ago

To add some details to this issue:

MediaManager 1.1.1 has a dependency on NuGet package Xam.Plugins.Android.ExoPlayer.UI 2.11.8, a wrapper around the Android ExoPlayer component. ExoPlayer 2.11 does not pass a required flag (FLAG_IMMUTABLE) when creating a broadcast intent, so on Android 12 (API 31) the player does not start a foreground service as it should.

This issue is a problem only when playing longish audio tracks. The app may work just fine when playing tracks of 10-15 minutes (depending on the device) or less. However, when playing longer tracks, if the device goes to sleep or the user navigates away from the app, after a certain point the OS kills the app because XMM has not started the foreground service that allows the app to continue running. For those of us with media players that need to play long audio tracks, it's a real showstopper.

I tried checking out the commit recommended as a starting point above (https://github.com/Baseflow/XamarinMediaManager/commit/24cf94f9a66a7fe6b3daf73198cb93fbe77745e8). This commit has a dependency on ExoPlayer 2.14.6, which should be compatible with Android 12. The first hurdle: I was unable to get the sample projects to build in VS 2022. The projects have errors related to MvvmCross logging similar to those I reported in #857. After attempting to address those, other errors cropped up and I gave up on the sample projects entirely, since it appears they're not in a usable state.

I created a Xamarin Forms project, referenced the MediaManager project, and added a Button to the MainPage that plays a clip. When I click the button, an exception is thrown from OnPrepare() in MediaManager\Platforms\Android\Player\PlaybackPreparer.cs:

**Java.Lang.AbstractMethodError:** 'abstract method "void com.google.android.exoplayer2.Player$EventListener.onMediaItemTransition(com.google.android.exoplayer2.MediaItem, int)"'

So, it appears that particular commit of XMM is not in a working state.

I tried updating the project to reference ExoPlayer 2.16.1, but I got build errors indicating that version does not support Xamarin Forms.

@martijn00, any suggestions for getting this component working on Android 12+ would be most welcome. Those of us who are using it for media players that play long tracks are stuck - unable to release a new version of our app that works properly due to Google's current API requirements for app updates in Google Play. I understand you're focused on getting XMM ported to Maui and may not have cycles to devote to getting it working yourself. But you might have an idea that could be helpful to those of us looking for a solution to the current impasse.

For those who are interested in the test I did, it's available here: https://github.com/sschaub/XamarinMediaManager/tree/android12-patch

ArchangelWTF commented 1 year ago

@sschaub That exception is very similar to the one thrown here: https://github.com/Baseflow/ExoPlayerXamarin/issues/137 which I've put a workaround for in that issue, I suppose you could try that or something similar and see if it works without throwing that exception then.

sschaub commented 1 year ago

@ArchangelWTF Thanks for the tip! I really appreciate your quick response.

I added the methods your workaround suggests to XamarinMediaManager\MediaManager\Platforms\Android\Player\PlayerEventListener.cs and was able to play an audio track successfully. I also noticed that a foreground service was started, which was very encouraging. However, when I added a MediaButtonReceiver to the AndroidManifest.xml to handle media button presses, the application crashed on startup in MediaBrowserService.cs (PrepareMediaSession()) on the line

var mediaSession = MediaManager.MediaSession = new MediaSessionCompat(this, nameof(MediaBrowserService));

with the error:

Java.Lang.IllegalArgumentException: 'com.companyname.formsdemo: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent ...

This is the same error that is causing the problem with XMM 1.1.1. Not sure what to do about it in this particular scenario, since MediaSessionCompat is in the Android libraries... I don't see a place to add the required flag near this line.

Also, I noticed that video playback is not working. I hear the audio of the video, but the video itself does not appear.

martijn00 commented 1 year ago

I've commited some fixes based on this. At least it compiles now. It would be great if someone can write the renderer to Maui.

irediaes commented 1 year ago

Hello @martijn00 @ArchangelWTF

Please, our app in production is current crashing for android 12+ users.

Throwing this error:

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent ...

Please, I am willing to pay to have a NuGet version that fixed this. It's super urgent.

I already tried building this but I am getting compile error on my Mac.

Looking forward to your positive feedback. Thank you!

arahmancsd commented 1 year ago

@irediaes If you are not concerned about the notification manager, things should be straightforward. I did update it without having the notification manager yet but working on my local version to solve the notification manager issue.

If you have a custom renderer or wherever you use those two flags in your Android Project (I used it in several places for notifications) So you should change them to the followings;

var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S)
                ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable
                : PendingIntentFlags.UpdateCurrent;
var pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, 0, intent, pendingIntentFlags);
irediaes commented 1 year ago

@arahmancsd

I am having compiling issues on my Mac msbuild while building local version.

"XamarinMediaManager/MediaManager.sln" (build target) (1) ->
"XamarinMediaManager/Samples/ElementPlayer.Forms.WPFCore/ElementPlayer.Forms.WPFCore.csproj" (default target) (16) ->
  /usr/local/share/dotnet/sdk/7.0.100/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.ImportWorkloads.props(14,38): 
error MSB4236: The SDK 'Microsoft.NET.SDK.WorkloadAutoImportPropsLocator' specified could not be found. [XamarinMediaManager/Samples/ElementPlayer.Forms.WPFCore/ElementPlayer.Forms.WPFCore.csproj]

Please can you share the compile NuGet of this plugin if you have one.

irediaes commented 1 year ago

Update:

I did some workaround and I was able to by-pass this issue with same Nuget version that was throw the error.

I was able to build for target SDK API 31+

irediaes commented 1 year ago

Currently, there's an issue with playing longer audio tracks that is more than 15-30min duration. Everything still points back to the plugin.

zohaibshahzad1990 commented 1 year ago

@irediaes oh wow! As I expected. There is no workaround. You were not listening to me.

zohaibshahzad1990 commented 1 year ago

This I was doing, directly fixing the issue by referencing the library as a project. @arahmancsd doing.

irediaes commented 1 year ago

@zohaibshahzad1990 I did a workaround on the app that got it working instead of the app crashing instantly when launched. At least, users can use the app and play the audios.

sschaub commented 1 year ago

I have had some success creating a patched version of XMM based on commit https://github.com/Baseflow/XamarinMediaManager/commit/24cf94f9a66a7fe6b3daf73198cb93fbe77745e8 that seems to work on Android 12. Here are the specific changes I made:

https://github.com/sschaub/XamarinMediaManager/commit/a04a73bc5908e8db3aceea7fb589b372d982d422

@martijn00, if you wanted to create a nuget release that would allow current Xamarin users to get their projects working with Android 12+, if you created a branch at that commit, I could submit a PR.

copang commented 1 year ago

@sschaub I think a lot of people are still using Xamarin while waiting for MAUI to be more stable, so it would be great if you could update this package for Xamarin for about 6-12 months.

janwiebe-jump commented 1 year ago

@sschaub I have been working on the ExoPlayer bindings for Xamarin for the latest version of ExoPlayerXamarin (2.18.7) at https://github.com/janwiebe-jump/ExoPlayerXamarin I got the basics of MediaManager (playing audio) working with this version as well. Maybe it can be of help for you.

sschaub commented 1 year ago

@copang I agree that it would be very helpful if this project could support Xamarin at least until its announced end of life of May 1, 2024 (https://dotnet.microsoft.com/en-us/platform/support/policy/xamarin). However I don't know what would be involved in having one code base support both Xamarin and MAUI. One issue is that current versions of the ExoPlayer bindings for Xamarin are compatible only with MAUI.

ArchangelWTF commented 1 year ago

@sschaub I'm currently working on a way to make the ExoPlayer bindings from the main repository build for both MAUI and MonoAndroid at the moment so that there shouldn't be need for any forks of that repository to keep updating ExoPlayer, as for how such backwards compatibility could be implemented in XamarinMediaManager I would have no idea.

sschaub commented 1 year ago

@ArchangelWTF That's great news!

martijn00 commented 1 year ago

The samples are up and running again. On Android Native sample i can use Exoplayer, but on the Maui sample something still doesn't work, but it shows.

Let me know if anyone finds a clue.

ArXamB commented 1 year ago

Did anyone Figure out how to actually create player notification for android 12+ ?

sschaub commented 1 year ago

@EchoSKSh @ArXamB I was able to solve the problem by forking the XMM project and patching it for Xamarin. Here is my patched version:

https://github.com/sschaub/XamarinMediaManager/commits/android12-patch-with-exoplayer2.14

abdoutech93 commented 1 year ago

@EchoSKSh @ArXamB I was able to solve the problem by forking the XMM project and patching it for Xamarin. Here is my patched version:

https://github.com/sschaub/XamarinMediaManager/commits/android12-patch-with-exoplayer2.14

even with this patch, still not working here is the details #893

EchoSKSh commented 1 year ago

@abdoutech93 I was able to fix this issue and it is working fine for me. You can check this repo - https://github.com/EchoSKSh/MediaManagerCustom

abdoutech93 commented 1 year ago

@EchoSKSh I think this repo is private !! I would also specify that I'm trying to play an hls stream.

abdoutech93 commented 1 year ago

@janwiebe-jump, I tried using your version and to be able to build I also had to remove some targets like in the above comment (I don't know if it caused any errors). However, it still doesn't create player in notification manager, I tested it on Oneplus and Xiaomi phones running Android 12. If you have successfully tested it, can you send me a copy of the package you used? Thanks!

Same, any update about it !!!?

janwiebe-jump commented 1 year ago

I use my own branch currently, that is suitable for targetSdk 32: https://github.com/janwiebe-jump/XamarinMediaManager/tree/feature/2023-xamarin-android-12

Not sure if HLS is working though, I only use it to play MP3 files

BajakiGabesz commented 1 year ago

You could use mine as well. It also supports HLS but is based on ExoPlayer 2.14.6 at the moment and it is working fine on Android 12 and earlier. I used part of the solution that @janwiebe-jump did. I also merged most of the fixes which arrived in the develop branch in the meantime and also have a workaround for issue #894.

You can find it here: https://github.com/BajakiGabesz/XamarinMediaManager/tree/xamarin-1.1.3

martijn00 commented 1 year ago

If anyone has done fixes, please contribute back as well so the MAUI version stays up-to-date.

ArchangelWTF commented 1 year ago

@martijn00 Maybe it would be an idea to create a Xamarin based branch on XamarinMediaManager so that we can merge all these forks we have floating around into one single codebase under one single nuget package much like we have done for ExoPlayer?

martijn00 commented 1 year ago

Fine with me!

15mgm15 commented 10 months ago

Update:

I did some workaround and I was able to by-pass this issue with same Nuget version that was throw the error.

I was able to build for target SDK API 31+

Can you share me your workarounds?