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

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

Closed
2 of 7 tasks
copang opened this issue Oct 10, 2022 · 41 comments
Closed
2 of 7 tasks

Comments

@copang
Copy link

copang commented Oct 10, 2022

🔙 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:

  • 📱 iOS
  • 🤖 Android
  • 🏁 WPF
  • 🌎 UWP
  • 🍎 MacOS
  • 📺 tvOS
  • 🐒 Xamarin.Forms
@arahmancsd
Copy link

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
Copy link
Contributor

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
Copy link
Author

copang commented Oct 17, 2022

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
Copy link
Contributor

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 24cf94f and manually do the change my PR did to the MediaBrowserService and then compile it for yourself to use in XF

@arahmancsd
Copy link

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

@janwiebe-jump
Copy link
Contributor

@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
Copy link
Author

copang commented Oct 19, 2022

@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
Copy link
Author

copang commented Oct 19, 2022

@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
Copy link
Contributor

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

@copang
Copy link
Author

copang commented Nov 1, 2022

@janwiebe-jump you can try workaround from #876

@sschaub
Copy link

sschaub commented Nov 21, 2022

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 (24cf94f). 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
Copy link
Contributor

@sschaub That exception is very similar to the one thrown here: Baseflow/ExoPlayerXamarin#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
Copy link

sschaub commented Nov 22, 2022

@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
Copy link
Member

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
Copy link

irediaes commented Dec 2, 2022

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
Copy link

@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
Copy link

irediaes commented Dec 2, 2022

@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
Copy link

irediaes commented Dec 3, 2022

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
Copy link

irediaes commented Dec 4, 2022

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
Copy link

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

@zohaibshahzad1990
Copy link

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

@irediaes
Copy link

irediaes commented Dec 6, 2022

@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
Copy link

sschaub commented Dec 7, 2022

I have had some success creating a patched version of XMM based on commit 24cf94f that seems to work on Android 12. Here are the specific changes I made:

sschaub@a04a73b

@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
Copy link
Author

copang commented Dec 8, 2022

@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
Copy link
Contributor

@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
Copy link

sschaub commented Dec 8, 2022

@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
Copy link
Contributor

@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
Copy link

sschaub commented Dec 8, 2022

@ArchangelWTF That's great news!

@martijn00
Copy link
Member

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
Copy link

ArXamB commented Dec 20, 2022

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

@sschaub
Copy link

sschaub commented Jan 18, 2023

@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
Copy link

@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
Copy link

@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
Copy link

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

@abdoutech93
Copy link

@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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Member

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

@ArchangelWTF
Copy link
Contributor

@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
Copy link
Member

Fine with me!

@15mgm15
Copy link

15mgm15 commented Aug 21, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests