Adding audio focus abandon. Solving a bug with the saving of the seek position.

This commit is contained in:
Tristan Roux
2019-04-06 19:41:25 +02:00
parent f227fcfa80
commit a8857877c3
8 changed files with 345 additions and 450 deletions

View File

@@ -609,9 +609,6 @@
<ItemGroup>
<AndroidResource Include="Resources\layout\HomePlaylists.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\HomeTopic.xml" />
</ItemGroup>
<ItemGroup>
<AndroidAsset Include="Assets\Version.txt" />
</ItemGroup>

View File

@@ -373,46 +373,6 @@ namespace Opus.Resources.Portable_Class
}
}
public async Task AddHomeTopics()
{
List<Song> channelLits = new List<Song>();
string nextPageToken = "";
while (nextPageToken != null)
{
try
{
YouTubeService youtube = YoutubeEngine.youtubeService;
SubscriptionsResource.ListRequest request = youtube.Subscriptions.List("snippet,contentDetails");
request.ChannelId = "UCRPb0XKQwDoHbgvtawH-gGw";
request.MaxResults = 50;
request.PageToken = nextPageToken;
SubscriptionListResponse response = await request.ExecuteAsync();
foreach (var item in response.Items)
{
Song channel = new Song(item.Snippet.Title.Substring(0, item.Snippet.Title.IndexOf(" - Topic")), item.Snippet.Description, item.Snippet.Thumbnails.Default__.Url, item.Snippet.ResourceId.ChannelId, -1, -1, null, true);
channelLits.Add(channel);
}
nextPageToken = response.NextPageToken;
}
catch(Exception ex)
{
Console.WriteLine("&ERROR FOUND (on home topics load) " + ex.Message);
return;
}
}
Random r = new Random();
List<Song> channels = channelLits.OrderBy(x => r.Next()).ToList();
channels.RemoveAll(x => selectedTopics.Contains(x.Title));
HomeSection TopicSelector = new HomeSection(Resources.GetString(Resource.String.music_genres), SectionType.TopicSelector, channels);
adapter.AddToList(new List<HomeSection> { TopicSelector });
}
public void AddQueue()
{
if (adapterItems[0].SectionTitle != "Queue")

View File

@@ -63,11 +63,6 @@ namespace Opus.Resources.Portable_Class
// View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.HomePlaylists, parent, false);
// return new LineSongHolder(itemView, OnClick, OnLongClick);
//}
else if(viewType == 3)
{
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.HomeTopic, parent, false);
return new LineSongHolder(itemView, OnClick, OnLongClick);
}
else
{
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ShuffleButton, parent, false);
@@ -213,8 +208,6 @@ namespace Opus.Resources.Portable_Class
return 1;
else if (items[position].contentType == SectionType.PlaylistList)
return 2;
else if (items[position].contentType == SectionType.TopicSelector)
return 3;
else
return 4;
}

View File

@@ -53,8 +53,9 @@ namespace Opus.Resources.Portable_Class
public static List<Song> queue = new List<Song>();
public static List<int> WaitForIndex = new List<int>();
public static List<Song> autoPlay = new List<Song>();
public MediaSessionCompat mediaSession;
public AudioManager audioManager;
private MediaSessionCompat mediaSession;
private AudioManager audioManager;
private AudioFocusRequestClass audioFocusRequest;
public NotificationManager notificationManager;
private bool noisyRegistered;
public static bool isRunning = false;
@@ -110,14 +111,14 @@ namespace Opus.Resources.Portable_Class
case "Pause":
if(isRunning)
Pause(true);
Pause();
else
Resume();
break;
case "ForcePause":
if (isRunning)
Pause(true);
Pause();
break;
case "ForceResume":
@@ -296,13 +297,13 @@ namespace Opus.Resources.Portable_Class
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
AudioFocusRequestClass focusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
audioFocusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
.SetAudioAttributes(attributes)
.SetAcceptsDelayedFocusGain(true)
.SetWillPauseWhenDucked(true)
.SetOnAudioFocusChangeListener(this)
.Build();
AudioFocusRequest audioFocus = audioManager.RequestAudioFocus(focusRequest);
AudioFocusRequest audioFocus = audioManager.RequestAudioFocus(audioFocusRequest);
if (audioFocus != AudioFocusRequest.Granted)
{
@@ -400,13 +401,13 @@ namespace Opus.Resources.Portable_Class
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
AudioFocusRequestClass focusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
audioFocusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
.SetAudioAttributes(attributes)
.SetAcceptsDelayedFocusGain(true)
.SetWillPauseWhenDucked(true)
.SetOnAudioFocusChangeListener(this)
.Build();
AudioFocusRequest audioFocus = audioManager.RequestAudioFocus(focusRequest);
AudioFocusRequest audioFocus = audioManager.RequestAudioFocus(audioFocusRequest);
if (audioFocus != AudioFocusRequest.Granted)
{
@@ -430,15 +431,15 @@ namespace Opus.Resources.Portable_Class
#pragma warning restore CS0618
}
if (progress != -1)
player.PlayWhenReady = true;
player.Prepare(mediaSource, true, true);
CreateNotification(song.Title, song.Artist, song.AlbumArt, song.Album);
if (progress != -1) //I'm seeking after the prepare because with some format, exoplayer's prepare reset the position
{
player.SeekTo(progress);
MainActivity.instance?.FindViewById<ImageButton>(Resource.Id.playButton).SetImageResource(Resource.Drawable.Pause);
}
player.PlayWhenReady = true;
player.Prepare(mediaSource, true, true);
CreateNotification(song.Title, song.Artist, song.AlbumArt, song.Album);
}
else
{
@@ -1018,7 +1019,7 @@ namespace Opus.Resources.Portable_Class
}
else
{
Pause(true);
Pause();
return;
}
}
@@ -1034,12 +1035,6 @@ namespace Opus.Resources.Portable_Class
{
Song song = await GetItem(position);
if (player == null)
InitializeService();
if (currentID == position && StartFromOldPosition)
player.SeekTo(LastTimer);
currentID = position;
if(showPlayer)
MainActivity.instance.ShowPlayer();
@@ -1067,7 +1062,7 @@ namespace Opus.Resources.Portable_Class
}
}
else
Play(song, -1, false);
Play(song, StartFromOldPosition ? LastTimer : -1, false);
Queue.instance?.RefreshAP();
}
@@ -1335,14 +1330,12 @@ namespace Opus.Resources.Portable_Class
StartForeground(notificationID, notification);
}
public void Pause(bool userRequested)
public void Pause()
{
if (userRequested)
ShouldResumePlayback = false;
ShouldResumePlayback = false;
if (!UseCastPlayer && player != null && isRunning)
{
SaveTimer(CurrentPosition);
isRunning = false;
Intent tmpPauseIntent = new Intent(Application.Context, typeof(MusicPlayer));
@@ -1579,6 +1572,7 @@ namespace Opus.Resources.Portable_Class
if (SaveQueue)
{
Console.WriteLine("&Saving the queue");
ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
ISharedPreferencesEditor editor = pref.Edit();
editor.PutInt("currentID", currentID);
@@ -1606,6 +1600,12 @@ namespace Opus.Resources.Portable_Class
}
}
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
audioManager.AbandonAudioFocusRequest(audioFocusRequest);
else
#pragma warning disable CS0618 // Type or member is obsolete
audioManager.AbandonAudioFocus(this);
#pragma warning restore CS0618 // Type or member is obsolete
MainActivity.instance.SkipStop = false;
noisyReceiver = null;
@@ -1628,6 +1628,8 @@ namespace Opus.Resources.Portable_Class
RemotePlayer.Stop();
StopSelf();
}
Player.instance?.Ready(); //Refresh play/pause state
}
private void SleepPause()
@@ -1656,12 +1658,11 @@ namespace Opus.Resources.Portable_Class
break;
case AudioFocus.Loss:
Pause(false);
ShouldResumePlayback = false;
Pause();
break;
case AudioFocus.LossTransient:
Pause(false);
Pause();
ShouldResumePlayback = true;
break;

View File

@@ -440,6 +440,8 @@ namespace Opus
void Sleep(int time)
{
time = 1; //This is for test
Intent intent = new Intent(MainActivity.instance, typeof(Sleeper));
intent.PutExtra("time", time);
MainActivity.instance.StartService(intent);

File diff suppressed because it is too large Load Diff

View File

@@ -1,49 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:elevation="6dp"
android:background="#fafafa"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="20dp" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:alpha=".8"
android:text="@string/recommendation_explanation" />
</LinearLayout>
<Button
android:id="@+id/viewMore"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Add Genres"
app:backgroundTint="?colorAccent"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lineRecycler" />
</LinearLayout>

View File

@@ -184,7 +184,7 @@
<string name="youtube_endpoint">L\'algorithme youtube a changé, l\'application ne peut plus lire cette vidéo. Attendez la prochaine mise a jour.</string>
<string name="timout">Timout, vérifier votre connection internet.</string>
<string name="unknow">Une erreur inconnue est survenue.</string>
<string name="country_blocked">Cette musique n'est pas disponible dans votre pays.</string>
<string name="country_blocked">Cette musique n\'est pas disponible dans votre pays.</string>
<string name="not_streamable"> ne peut pas être lu, l\'audio n\'est pas disponible.</string> <!--//A song title will be placed before this sentence-->
<string name="update_no_internet">Vous n\'êtes pas connecté a internet, impossible de verifier si des mises à jour existent.</string>
<string name="update">La version %1$s est disponible</string> <!--//%1$s will be replaced with the version number-->