开发者

Is there a way to make midi sounds in my Android app?

开发者 https://www.devze.com 2023-03-13 09:51 出处:网络
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 th开发者_如何学Pythonem in the apk.

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 th开发者_如何学Pythonem 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消