Skip to content

Commit

Permalink
Fix Android song unable to find a specified URI
Browse files Browse the repository at this point in the history
- `Assets.OpenFd` throw if the given filename does not exist.
- So a hack to try `OpenFd` if not `SetDataSource` next with cascading `try/catch`.
  • Loading branch information
Mindfulplays committed Dec 29, 2023
1 parent 5fc1d04 commit 043ea41
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions MonoGame.Framework/Platform/Media/Song.Android.cs
Expand Up @@ -82,11 +82,25 @@ internal void Play(TimeSpan? startPosition)
}
else
{
var afd = Game.Activity.Assets.OpenFd(_name);
if (afd == null)
return;

_androidPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
// Try the game's assets if we are using the ContentManager, otherwise, try
// if the name refers to a file directly..
try
{
var afd = Game.Activity.Assets.OpenFd(_name);
_androidPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
}
catch (Exception)
{
try
{
_androidPlayer.SetDataSource(_name);
}
catch (Exception e)
{
Console.WriteLine(e);
// At this point, it's too late to do anything, so log an error (silently).
}
}
}


Expand Down

0 comments on commit 043ea41

Please sign in to comment.