为什么MidiInputPort.flush()不清除Android中的缓冲区?

时间:2016-09-07 22:49:25

标签: android midi

我正在使用android.media.midi,我正在向MidiInputPort发送一堆midi数据,其延迟值如下:

long start = System.nanoTime();
if (messages != null)
{
    for (int i = 0; i < messages.length(); i++)
    {

        MidiNote note = MidiHelper.parseMessageForNote(messages.getString(i));
        if (note != null)
            {
                byte[] buffer = new byte[32];
                int numBytes = 0;
                int channel = 1; // MIDI channels 1-16 are encoded as 0-15.
                buffer[numBytes++] = (byte) (note.action + (channel - 1));
                buffer[numBytes++] = (byte) note.note;
                buffer[numBytes++] = (byte) note.velocity; 

                long delay = note.delay * 1000000;

                midiInputPort.send(buffer, 0, numBytes, start + delay);

                start = start + delay;
            }

     }

     midiInputPort.flush();
}

你会注意到我在发送所有数据后立即调用了flush(只是试图让flush()工作),但它没有效果。数据仍然被发送到Midi端口,好像我从未调用过flush。该功能的文档非常简单明了。它说&#34;如果你想取消你将来安排的事件,那么请调用flush()。&#34;有什么关于这件事,我想念吗?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

输出流是否仍然打开?也许您正在发送刷新数据包的MIDI设备不支持它?

查看google源代码,flush()调用MidiPortImpl.packFlush(),只要它仍处于打开状态,就会向输出流发送byte [1] = 0x02的单字节数组。

public void onFlush() throws IOException {
    synchronized (mBuffer) {
        if (mOutputStream == null) {
            throw new IOException("MidiInputPort is closed");
        }
        int length = MidiPortImpl.packFlush(mBuffer);
        mOutputStream.write(mBuffer, 0, length);
    }
}

https://android.googlesource.com/platform/frameworks/base/+/master/media/java/android/media/midi/MidiInputPort.java

https://android.googlesource.com/platform/frameworks/base/+/master/media/java/android/media/midi/MidiPortImpl.java