0

I'm trying to make an app that can play different midi files at the same time. The files would not be streaming and I would like to include them in the apk.

A maximum of 12 would be played at the same time... Mp3 or a combination of both would also be a suitable substitute but for now midi would be ideal.

Is this at all possible? Thanks in advance to the stack-overflow geniuses! :)

-EltMrx

EltMrx
  • 37
  • 2
  • 14

2 Answers2

3

One easy way to play a single sound is to use MediaPlayer. Put your sound files in the /res/raw folder, then call the below method using R constants, e.g. playSound(R.raw.sound_file_name) where playSound looks something like this:

private void playSound(int soundResId) {
        MediaPlayer mp = MediaPlayer.create(context, soundResId);
        if (mp == null) {
            Log.warn("playSound", "Error creating MediaPlayer object to play sound.");
            return;
        }

        mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e("playSound", "Found an error playing media. Error code: " + what);
                mp.release();
                return true;
            }
        });

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });

        mp.start();
    }

Now, playing multiple sounds at the same time is a bit more complex, but there is a good solution here.

Community
  • 1
  • 1
plowman
  • 13,335
  • 8
  • 53
  • 53
2

As @uncheck noted, you can use the standard Android MediaPlayer class for MP3, though playing multiple channels at once is a bit tricky.

Android does not have a built-in synthesizer, so if you want to play pure MIDI files through some type of instrument, your best bet would be to use libpd for Android. After that, you can probably find a PD patch with a synth that would fit your needs for the given sound that you're after.

Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
  • Well, libpd is nice, but android does have a midi synthesizer that is used by `MediaPlayer`. It uses the built-in Sonivox midisynth. However, this synth is only accessible through the `MediaPlayer` and not directly for streaming midi events etc. – Peterdk Aug 17 '11 at 14:55