How to get local .mp3 file

Hello

How do you include a mp3 file into the bundle? Or how do you make this work with a local mp3 file:

    static object Load(Context c, object[] args)
        {

            var player = _players[(int)args[0]];

            var filePath = args[1] as string;

            debug_log "Load file: "+filePath;

            foreach(var bf in Uno.IO.Bundle.AllFiles)
            {

                //debug_log bf.SourcePath;
            }

            //filePath = "sound.mp3"; File only included in project - not working
            //filePath = "http://www.wallandermedia.se/sound.mp3"; Works 


            player.Load(filePath);

            return null;
        }

Hi!

It looks like you are using the MediaPlayer code from https://www.fusetools.com/community/forums/howto_discussions/audio_player_implentation_with_fusejs?page=1

In the player code we are using Java/ObjectiveC code for the implementation, so we have to fish out some stuff that they want.

I have added the code that is needed to load bundlefiles for this particular case in Android/iOS

interface IMediaPlayer
{
    void Load(string filePath);
    void Load(BundleFile bundleFile);
    void Play();
}

extern(Android) class AndroidMediaPlayer : IMediaPlayer
{
    public void Load(BundleFile bundleFile)
    {
        LoadBundle(_mediaPlayerHandle, bundleFile.BundlePath);
    }

    [Foreign(Language.Java)]
    static void LoadBundle(Java.Object handle, string bundlePath)
    @{
        try
        {
            android.content.res.AssetFileDescriptor afd = @(Activity.Package).@(Activity.Name)
                .GetRootActivity()
                .getAssets()
                .openFd(assetName);

            ((android.media.MediaPlayer)handle).setDataSource(
                afd.getFileDescriptor(),
                afd.getStartOffset(),
                afd.getLength());

            ((android.media.MediaPlayer)handle).prepare();
        }
        catch (java.lang.Exception e)
        {
            android.util.Log.d("android player", "Failed to load: " + e.getMessage());
        }
    @}
}

extern(iOS) class iOSMediaPlayer : IMediaPlayer
{
    public void Load(BundleFile bundleFile)
    {
        var absolutePath = GetBundleAbsolutePath(bundleFile.BundlePath);
    }

    [Foreign(Language.ObjC)]
    static string GetBundleAbsolutePath(string bundlePath)
    @{
        return [[[NSBundle mainBundle] URLForResource:bundlePath withExtension:@""] absoluteString];
    @}
}

Let me know if you have any further questions :slight_smile: