Skip to content

Commit

Permalink
Notifications on android
Browse files Browse the repository at this point in the history
  • Loading branch information
martijn00 committed Jul 2, 2019
1 parent f3de688 commit 01f0b0d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
4 changes: 3 additions & 1 deletion MediaManager/Platforms/Android/MediaManagerImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Android.App;
using Android.Content;
using Android.Support.V4.Media.Session;
using Com.Google.Android.Exoplayer2;
using MediaManager.Media;
using MediaManager.Platforms.Android;
using MediaManager.Platforms.Android.Media;
Expand All @@ -22,7 +23,7 @@
namespace MediaManager
{
[global::Android.Runtime.Preserve(AllMembers = true)]
public class MediaManagerImplementation : MediaManagerBase
public class MediaManagerImplementation : MediaManagerBase, IMediaManager<SimpleExoPlayer>
{
public MediaManagerImplementation()
{
Expand Down Expand Up @@ -108,6 +109,7 @@ public override IMediaPlayer MediaPlayer
}

public AndroidMediaPlayer AndroidMediaPlayer => (AndroidMediaPlayer)MediaPlayer;
public SimpleExoPlayer Player => AndroidMediaPlayer.Player;

private IVolumeManager _volumeManager;
public override IVolumeManager VolumeManager
Expand Down
25 changes: 21 additions & 4 deletions MediaManager/Platforms/Android/MediaSession/MediaBrowserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected PlayerNotificationManager PlayerNotificationManager {

public readonly string ChannelId = "audio_channel";
public readonly int ForegroundNotificationId = 1;
public bool IsForeground = false;


public MediaBrowserService()
{
Expand All @@ -44,6 +46,20 @@ public override void OnCreate()

PrepareMediaSession();
PrepareNotificationManager();

/*MediaManager.AndroidMediaPlayer.PlayerEventListener.OnPlayerStateChangedImpl = (bool playWhenReady, int playbackState) =>
{
if (playWhenReady && !IsForeground)
{
//TODO: Start the service again.
//StartForeground()
}
else if (!playWhenReady && IsForeground)
{
StopForeground(false);
IsForeground = false;
}
};*/
}

protected virtual void PrepareMediaSession()
Expand All @@ -56,8 +72,6 @@ protected virtual void PrepareMediaSession()

MediaManager.MediaSession.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons |
MediaSessionCompat.FlagHandlesTransportControls);

//MediaManager.AndroidMediaPlayer.MediaSession = MediaSession;
}

protected virtual void PrepareNotificationManager()
Expand All @@ -76,24 +90,26 @@ protected virtual void PrepareNotificationManager()
{
ContextCompat.StartForegroundService(ApplicationContext, new Intent(ApplicationContext, Java.Lang.Class.FromType(typeof(MediaBrowserService))));
StartForeground(notificationId, notification);
IsForeground = true;
};
NotificationListener.OnNotificationCancelledImpl = (notificationId) =>
{
//TODO: in new exoplayer use StopForeground(false) or use dismissedByUser
StopForeground(true);
StopSelf();
IsForeground = false;
};

PlayerNotificationManager.SetFastForwardIncrementMs((long)MediaManager.StepSize.TotalMilliseconds);
PlayerNotificationManager.SetRewindIncrementMs((long)MediaManager.StepSize.TotalMilliseconds);
PlayerNotificationManager.SetNotificationListener(NotificationListener);
PlayerNotificationManager.SetMediaSessionToken(SessionToken);
PlayerNotificationManager.SetOngoing(true);
PlayerNotificationManager.SetOngoing(false);
PlayerNotificationManager.SetUsePlayPauseActions(MediaManager.NotificationManager.ShowPlayPauseControls);
PlayerNotificationManager.SetUseNavigationActions(MediaManager.NotificationManager.ShowNavigationControls);

//Must be called to start the connection
(MediaManager.NotificationManager as Notifications.NotificationManager).Player = MediaManager.AndroidMediaPlayer.Player;
(MediaManager.NotificationManager as Notifications.NotificationManager).Player = MediaManager.Player;
//PlayerNotificationManager.SetPlayer(MediaManager.AndroidMediaPlayer.Player);
}

Expand All @@ -117,6 +133,7 @@ public override void OnDestroy()
MediaManager.MediaSession.Release();
//MediaSession = null;
StopForeground(true);
IsForeground = false;
}

public override BrowserRoot OnGetRoot(string clientPackageName, int clientUid, Bundle rootHints)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class NotificationManager : NotificationManagerBase
{
protected MediaManagerImplementation MediaManager = CrossMediaManager.Android;

public NotificationManager()
{

}

public PlayerNotificationManager PlayerNotificationManager { get; set; }

private IPlayer _player;
Expand All @@ -29,10 +34,6 @@ internal IPlayer Player
}
}

public NotificationManager()
{
}

public override bool Enabled
{
get => base.Enabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected AndroidMediaPlayer(IntPtr handle, JniHandleOwnership transfer) : base(
protected QueueEditorMediaSourceFactory MediaSourceFactory { get; set; }
protected TimelineQueueEditor TimelineQueueEditor { get; set; }
protected MediaSessionConnectorPlaybackPreparer PlaybackPreparer { get; set; }
protected PlayerEventListener PlayerEventListener { get; set; }
public PlayerEventListener PlayerEventListener { get; set; }
protected RatingCallback RatingCallback { get; set; }

private SimpleExoPlayer _player;
Expand Down

0 comments on commit 01f0b0d

Please sign in to comment.