Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/media/midi/MidiReceiver#pubctors) \| [Methods](https://developer.android.com/reference/android/media/midi/MidiReceiver#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/midi/MidiReceiver#inhmethods)

# MidiReceiver

*** ** * ** ***

` public abstract class MidiReceiver `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.midi.MidiReceiver |

Known direct subclasses [MidiInputPort](https://developer.android.com/reference/android/media/midi/MidiInputPort) [MidiInputPort](https://developer.android.com/reference/android/media/midi/MidiInputPort) This class is used for sending data to a port on a MIDI device   

*** ** * ** ***

Interface for sending and receiving data to and from a MIDI device.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/media/midi/MidiReceiver#MidiReceiver()() ` Default MidiReceiver constructor. |
| ` https://developer.android.com/reference/android/media/midi/MidiReceiver#MidiReceiver(int)(int maxMessageSize) ` MidiReceiver constructor. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/media/midi/MidiReceiver#flush()() ` Instructs the receiver to discard all pending MIDI data. |
| ` final int` | ` https://developer.android.com/reference/android/media/midi/MidiReceiver#getMaxMessageSize()() ` Returns the maximum size of a message this receiver can receive. |
| ` void` | ` https://developer.android.com/reference/android/media/midi/MidiReceiver#onFlush()() ` Called when the receiver is instructed to discard all pending MIDI data. |
| ` abstract void` | ` https://developer.android.com/reference/android/media/midi/MidiReceiver#onSend(byte[],%20int,%20int,%20long)(byte[] msg, int offset, int count, long timestamp) ` Called whenever the receiver is passed new MIDI data. |
| ` void` | ` https://developer.android.com/reference/android/media/midi/MidiReceiver#send(byte[],%20int,%20int)(byte[] msg, int offset, int count) ` Called to send MIDI data to the receiver without a timestamp. |
| ` void` | ` https://developer.android.com/reference/android/media/midi/MidiReceiver#send(byte[],%20int,%20int,%20long)(byte[] msg, int offset, int count, long timestamp) ` Called to send MIDI data to the receiver with a specified timestamp. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### MidiReceiver

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MidiReceiver ()
```

Default MidiReceiver constructor. Maximum message size is set to `https://developer.android.com/reference/java/lang/Integer#MAX_VALUE`

<br />

### MidiReceiver

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MidiReceiver (int maxMessageSize)
```

MidiReceiver constructor.

<br />

| Parameters ||
|---|---|
| `maxMessageSize` | `int`: the maximum size of a message this receiver can receive <br /> |

## Public methods

### flush

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void flush ()
```

Instructs the receiver to discard all pending MIDI data.

<br />

| Throws ||
|---|---|
|   | java.io.IOException |
| `https://developer.android.com/reference/java/io/IOException` |   |

### getMaxMessageSize

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int getMaxMessageSize ()
```

Returns the maximum size of a message this receiver can receive.

<br />

| Returns ||
|---|---|
| `int` | maximum message size <br /> |

### onFlush

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onFlush ()
```

Called when the receiver is instructed to discard all pending MIDI data. Subclasses should override this method if they maintain a list or queue of MIDI data to be processed in the future.

<br />

| Throws ||
|---|---|
|   | java.io.IOException |
| `https://developer.android.com/reference/java/io/IOException` |   |

### onSend

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void onSend (byte[] msg, 
                int offset, 
                int count, 
                long timestamp)
```

Called whenever the receiver is passed new MIDI data. Subclasses override this method to receive MIDI data. May fail if count exceeds `https://developer.android.com/reference/android/media/midi/MidiReceiver#getMaxMessageSize()`. NOTE: the msg array parameter is only valid within the context of this call. The msg bytes should be copied by the receiver rather than retaining a reference to this parameter. Also, modifying the contents of the msg array parameter may result in other receivers in the same application receiving incorrect values in their {link #onSend} method.

<br />

| Parameters ||
|---|---|
| `msg` | `byte`: a byte array containing the MIDI data <br /> |
| `offset` | `int`: the offset of the first byte of the data in the array to be processed <br /> |
| `count` | `int`: the number of bytes of MIDI data in the array to be processed <br /> |
| `timestamp` | `long`: the timestamp of the message (based on `https://developer.android.com/reference/java/lang/System#nanoTime()` <br /> |

| Throws ||
|---|---|
|   | java.io.IOException |
| `https://developer.android.com/reference/java/io/IOException` |   |

### send

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void send (byte[] msg, 
                int offset, 
                int count)
```

Called to send MIDI data to the receiver without a timestamp. Data will be processed by receiver in the order sent. Data will get split into multiple calls to `https://developer.android.com/reference/android/media/midi/MidiReceiver#onSend(byte[],%20int,%20int,%20long)` if count exceeds `https://developer.android.com/reference/android/media/midi/MidiReceiver#getMaxMessageSize()`. Blocks until all the data is sent or an exception occurs. In the latter case, the amount of data sent prior to the exception is not provided to caller. The communication should be considered corrupt. The sender should reestablish communication, reset all controllers and send all notes off.

<br />

| Parameters ||
|---|---|
| `msg` | `byte`: a byte array containing the MIDI data <br /> |
| `offset` | `int`: the offset of the first byte of the data in the array to be sent <br /> |
| `count` | `int`: the number of bytes of MIDI data in the array to be sent <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the data could not be sent in entirety |

### send

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void send (byte[] msg, 
                int offset, 
                int count, 
                long timestamp)
```

Called to send MIDI data to the receiver with a specified timestamp. Data will be processed by receiver in order first by timestamp, then in the order sent. Data will get split into multiple calls to `https://developer.android.com/reference/android/media/midi/MidiReceiver#onSend(byte[],%20int,%20int,%20long)` if count exceeds `https://developer.android.com/reference/android/media/midi/MidiReceiver#getMaxMessageSize()`. Blocks until all the data is sent or an exception occurs. In the latter case, the amount of data sent prior to the exception is not provided to caller. The communication should be considered corrupt. The sender should reestablish communication, reset all controllers and send all notes off.

<br />

| Parameters ||
|---|---|
| `msg` | `byte`: a byte array containing the MIDI data <br /> |
| `offset` | `int`: the offset of the first byte of the data in the array to be sent <br /> |
| `count` | `int`: the number of bytes of MIDI data in the array to be sent <br /> |
| `timestamp` | `long`: the timestamp of the message, based on `https://developer.android.com/reference/java/lang/System#nanoTime()` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the data could not be sent in entirety |