atsushieno / managed-midi

[Past project] Cross-platform MIDI processing library for mono and .NET (ALSA, CoreMIDI, Android, WinMM and UWP).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to start midi - Android (Xamarin - C#))

jonathanneels opened this issue · comments

Good day,

I try to play a sound on my android device, through an Xamarin C# application (build version focussed on Android 8.1).

Unfortunately when I start the app to play the piano sound I get "Error: sequence contains no elements".

This is my (slim) code:

                var access = MidiAccessManager.Default;
                var output = access.OpenOutputAsync(access.Outputs.Last().Id).Result;  
                output.Send(new byte[] { 0xC0, GeneralMidi.Instruments.AcousticGrandPiano }, 0, 2, 0);   
                output.CloseAsync();

I believe the error comes from " var output = access.OpenOutputAsync(access.Outputs.Last().Id).Result; "

I have no idea how to generate a midi port id (if this would be the root of the problem).

Can you help me to play a piano note?

Thank you very much in advance.

I often write such a hacky sample, but Last() will throw that typical Linq exception if there is no such item in the sequence (IEnumerable, here access.Outputs).

You need a working MIDI device in Android MIDI API manner, which is either USB MIDI device, BLE MIDI device, or MidiDeviceService. https://developer.android.com/reference/android/media/midi/package-summary

The same kind of situation (i.e. lack of MIDI devices) happens to macOS too.

Thank you for your quick response!

I'll try it out.

Feel free to ask questions here, but I will close this issue so far. Thanks.