Author Topic: How to load mid & sf2 files from 'assets' folder? (cocos2d-x on android, Native)  (Read 143 times)


  • Posts: 10

I'm having issue with playing midi file using soundfont file on Android Studio project of cocos2d-x 3.16

Code: [Select]

    CCLOG("%s", FileUtils::getInstance()->fullPathForFilename("test.mid").c_str());   // LOG: "assets/test.mid"

    chan = BASS_MIDI_StreamCreateFile(false, FileUtils::getInstance()->fullPathForFilename("test.sf2").c_str(), 0, 0, BASS_SAMPLE_LOOP, 48000);
    CCLOG("[BASS_MIDI_StreamCreateFile] %d", BASS_ErrorGetCode() );   // Error Code is 2 (BASS_ERROR_FILEOPEN)

    CCLOG("%s", FileUtils::getInstance()->fullPathForFilename("test.sf2").c_str());   // LOG: "assets/test.sf2"
    font = BASS_MIDI_FontInit(FileUtils::getInstance()->fullPathForFilename("test.sf2").c_str(), 0);
    CCLOG("[BASS_MIDI_FontInit] %d", BASS_ErrorGetCode() );   // Error Code is 2 (BASS_ERROR_FILEOPEN)

    sf.font = font;
    sf.preset = -1; = 0;
    BASS_MIDI_StreamSetFonts(chan, &sf, 1);
    CCLOG("[BASS_MIDI_StreamSetFonts] %d", BASS_ErrorGetCode() ); // Error Code is 20 (BASS_ERROR_ILLPARAM)

    BASS_ChannelPlay(chan, false);
    CCLOG("[BASS_ChannelPlay] %d", BASS_ErrorGetCode() ); // Error Code is 5 (BASS_ERROR_HANDLE)

This works without any errors on actual iOS device, and assets(.mid & .sf2) are in the right folder so that I got no error (Errorcode 0) when I tried BASS_MIDI_StreamCreateFile with passing data of midifile, not the location of file(mem=true, on Android).
It seems BASS_MIDI_FontInit only accept the location of file.

Did I miss something to locate media files in 'assets' folder inside apk, for bass library?
or is there any workaround for this? please check into that for me.

Thank in advance.

« Last Edit: 1 Feb '18 - 07:10 by loom »

Ian @ un4seen

  • Administrator
  • Posts: 20616
I don't think it is possible to access assets directly, rather they need to be accessed via AssetManager. BASS provides an Asset class to access assets but it will only work with files that are stored rather than compressed in the app's APK file, ie. if they're compressed already. I'm not sure that will be the case with a MIDI file or soundfont (both are likely to be compressible). If they can't be accessed via the Asset class, then you have a couple of options. You could extract them to files and pass those files to BASSMIDI, which has the advantage of only having to extract the files once (you can subsequently always use the already extracted files). The other option is to extract them to memory (eg. a ByteBuffer) and pass that to BASSMIDI. In that case, you would need to use BASS_MIDI_FontInitUser (rather than BASS_MIDI_FontInit) to load the soundfont.


  • Posts: 10
Hi Ian,

Thanks for letting me know current situation.
Can I use BASS_MIDI_FontInit with BASS_MIDI_FONT_MEM flag? I was wondering if that flag was deprecated because .chm document doesn't contain the explanation of BASS_MIDI_FONT_MEM.
By the way, we can have uncompressed soundfont file in the app's APK file when we change the extention from 'sf2' to 'png' or some other well-known 'already-compressed' formats.

I think it'll be handy if we could have access to assets in the APK since almost every android developers using native BASS & BASSMIDI api would love to use it for playing their media files from the asset folder inside APK.
(for example, FMOD is providing special prefix 'file:///android_asset/' for doing that since version 5, FMOD Studio & Low Level API).


« Last Edit: 2 Feb '18 - 03:28 by loom »