Skip to content

Commit

Permalink
Add Media Session
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Aug 13, 2023
1 parent d730427 commit 910ede0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions AniStream/AniStream.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<PackageReference Include="Square.Picasso" Version="2.71828.0" />
<PackageReference Include="Xam.Plugins.Android.ExoPlayer" Version="2.18.11" />
<PackageReference Include="Xam.Plugins.Android.ExoPlayer.OkHttp" Version="2.18.11" />
<PackageReference Include="Xam.Plugins.Android.ExoPlayer.MediaSession" Version="2.18.11" />
<PackageReference Include="Xamarin.Android.Flowlayout" Version="1.0.0.4" />
<PackageReference Include="Xamarin.Firebase.Auth" Version="121.1.0" />
<PackageReference Include="Xamarin.Firebase.Crashlytics" Version="118.3.2.1" />
Expand Down
45 changes: 35 additions & 10 deletions AniStream/VideoActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Android.Graphics.Drawables;
using Android.Media.Audiofx;
using Android.OS;
using Android.Support.V4.Media.Session;
using Android.Util;
using Android.Views;
using Android.Views.Animations;
Expand All @@ -29,6 +30,7 @@
using Bumptech.Glide;
using Com.Google.Android.Exoplayer2;
using Com.Google.Android.Exoplayer2.Audio;
using Com.Google.Android.Exoplayer2.Ext.Mediasession;
using Com.Google.Android.Exoplayer2.Ext.Okhttp;
using Com.Google.Android.Exoplayer2.Extractor;
using Com.Google.Android.Exoplayer2.Metadata;
Expand Down Expand Up @@ -80,6 +82,10 @@ public class VideoActivity : ActivityBase, IPlayer.IListener, ITrackNameProvider
//private PlayerView playerView = default!;
DefaultTrackSelector trackSelector = default!;

//private MediaSession? MediaSession { get; set; }
private MediaSessionCompat? MediaSession { get; set; }
private MediaSessionConnector? MediaSessionConnector { get; set; }

private ProgressBar progressBar = default!;
private ImageButton exoplay = default!;
private ImageButton exoQuality = default!;
Expand Down Expand Up @@ -184,9 +190,11 @@ protected async override void OnCreate(Bundle? savedInstanceState)

NextButton.Click += (s, e) => PlayNextEpisode();

/*var audioFocusChangeListener = new AudioFocusChangeListener();
var audioManager = (Android.Media.AudioManager?)ApplicationContext?.GetSystemService(AudioService);
//var audioManager = Android.Media.AudioManager.FromContext(this);

var audioFocusChangeListener = new AudioFocusChangeListener();

audioFocusChangeListener.OnAudioFocusChanged += (_, focus) =>
{
switch (focus)
Expand All @@ -201,6 +209,15 @@ protected async override void OnCreate(Bundle? savedInstanceState)
}
};

#pragma warning disable CA1422
audioManager!.RequestAudioFocus(
audioFocusChangeListener,
(Android.Media.Stream)ContentType.Movie,
//Android.Media.Stream.Music,
AudioFocus.Gain
);
#pragma warning restore CA1422

var audioAttributes = new Android.Media.AudioAttributes.Builder()
.SetUsage(Android.Media.AudioUsageKind.Media)!
.SetContentType(Android.Media.AudioContentType.Movie)!
Expand All @@ -212,15 +229,7 @@ protected async override void OnCreate(Bundle? savedInstanceState)
.SetOnAudioFocusChangeListener(audioFocusChangeListener)
.Build()!;

#pragma warning disable CA1422
//audioManager?.RequestAudioFocus(
// audioFocusChangeListener,
// (Android.Media.Stream)ContentType.Movie,
// AudioFocus.Gain
//);
audioManager?.RequestAudioFocus(focusRequest);
#pragma warning restore CA1422*/

var settingsButton = FindViewById<ImageButton>(Resource.Id.exo_settings)!;
SourceButton = FindViewById<ImageButton>(Resource.Id.exo_source)!;
Expand Down Expand Up @@ -565,6 +574,19 @@ private void BuildExoplayer(CacheDataSource.Factory cacheFactory)
.Build()!;

playerView.Player = exoPlayer;

try
{
MediaSession = new(this, "AniStreamMediaSession");

MediaSessionConnector = new MediaSessionConnector(MediaSession);
MediaSessionConnector.SetPlayer(exoPlayer);
}
catch (Exception e)
{
this.ShowToast(e.Message);
}

exoPlayer.AddListener(this);

if (playerView.SubtitleView is not null)
Expand Down Expand Up @@ -1107,6 +1129,9 @@ public void OnPlaybackStateChanged(int playbackState)
{
var isPlaying = exoPlayer.IsPlaying;
}

if (playbackState == IPlayer.StateEnded)
PlayNextEpisode();
}

public void OnPlaybackSuppressionReasonChanged(int playbackSuppressionReason)
Expand Down

0 comments on commit 910ede0

Please sign in to comment.