如何使用 Android MIDI 包将 MIDI 发送到自己的虚拟输出端口?

How to send MIDI to own virtual output port with Android MIDI Package?

我正在创建一个使用 Android MIDI 包的 Java Android 应用程序: https://developer.android.com/reference/android/media/midi/package-summary#extend_midideviceservice

应用程序需要部署自己的虚拟 MIDI 输入和输出端口。 到目前为止,输入端运行良好。我可以部署输出端口,它在其他应用程序中可见,但我不知道如何发送到输出端口,也找不到合适的文档。

有没有人documentation/example打开和使用自部署输出端口?

我的device_info.xml:

<devices>
    <device
        manufacturer="anyManufacturer"
        product="Virtual"
        name="myApp Virtual Inport">
        <input-port name="input" />
    </device>
    <device
        manufacturer="anyManufacturer"
        product="Virtual"
        name="myApp Virtual Outport">
        <output-port name="output" />
    </device>
</devices>

我自己的解决方案:

XML:

<devices>
    <device
        manufacturer="anyManufacturer"
        product="Virtual"
        name="myApp Virtual Port">
        <input-port name="input" />
        <output-port name="output" />
    </device>
</devices>

然后在服务中调用getOutputPortReceivers()获取输出端口。 https://developer.android.com/reference/android/media/midi/MidiDeviceService#getOutputPortReceivers()

然后你可以在那个 MidiReceiver 上使用 send()。

public class MIDIConnectDeviceService extends MidiDeviceService {
    private MidiReceiver mInputReceiver = new MyReceiver();
    private static MidiReceiver mOutputReceiver; 

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    // Declare the receivers associated with your input ports.
    public MidiReceiver[] onGetInputPortReceivers() {
        return new MidiReceiver[] { mInputReceiver };
    }    

    public static void send(byte[] msg, int offset, int count, long timestamp) throws IOException {
        if (mOutputReceiver != null){
            mOutputReceiver.send(msg, offset, count, timestamp);
        }
    }

    class MyReceiver extends MidiReceiver {
        @Override
        public void onSend(byte[] data, int offset, int count,
                           long timestamp) throws IOException {

        }
    }

    @Override
    public void onDeviceStatusChanged(MidiDeviceStatus status) {
        MidiReceiver[] midiReceivers = getOutputPortReceivers();
        mOutputReceiver = midiReceivers[0];
    }
}