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 Feb 6, 2024
1 parent 5fc1d04 commit 8e6782e
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions MonoGame.Framework/Platform/Media/Song.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,22 @@ internal void Play(TimeSpan? startPosition)

if (assetUri != null)
{
// Check if we have a direct asset URI.
_androidPlayer.SetDataSource(MediaLibrary.Context, this.assetUri);
}
else if (_name.StartsWith("file://"))
{
// Otherwise, check if this is a file URI.
_androidPlayer.SetDataSource(_name);
}
else
{
var afd = Game.Activity.Assets.OpenFd(_name);
if (afd == null)
return;

_androidPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
// Otherwise, assume it's a file path. (This might throw if the file doesn't exist)
var afd = Game.Activity?.Assets?.OpenFd(_name);
if (afd != null)
{
_androidPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
}
}


Expand Down

0 comments on commit 8e6782e

Please sign in to comment.