MIDIPort
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The MIDIPort interface of the Web MIDI API represents a MIDI input or output port.
A MIDIPort instance is created when a new MIDI device is connected. Therefore it has no constructor.
Properties
-
MIDIPort.idRead only -
Returns a
stringcontaining the unique ID of the port. -
MIDIPort.manufacturerRead only -
Returns a
stringcontaining the manufacturer of the port. -
MIDIPort.nameRead only -
Returns a
stringcontaining the system name of the port. -
MIDIPort.typeRead only -
Returns a
stringcontaining the type of the port, one of:"input"-
The
MIDIPortis an input port. "output"-
The
MIDIPortis an output port.
-
MIDIPort.versionRead only -
Returns a
stringcontaining the version of the port. -
MIDIPort.stateRead only -
Returns a
stringcontaining the state of the port, one of:"disconnected"-
The device that this
MIDIPortrepresents is disconnected from the system. "connected"-
The device that this
MIDIPortrepresents is currently connected.
-
MIDIPort.connectionRead only -
Returns a
stringcontaining the connection state of the port, one of:"open"-
The device that this
MIDIPortrepresents has been opened and is available. "closed"-
The device that this
MIDIPortrepresents has not been opened, or has been closed. "pending"-
The device that this
MIDIPortrepresents has been opened but has subsequently disconnected .
Event handlers
MIDIPort.onstatechange-
Called when an existing port changes its state or connection.
Methods
This interface also inherits methods from EventTarget.
MIDIPort.open()-
Makes the MIDI device connected to this
MIDIPortexplicitly available, and returns aPromisewhich resolves once access to the port has been successful. MIDIPort.close()-
Makes the MIDI device connected to this
MIDIPortunavailable, changing thestatefrom"open"to"closed". This returns aPromisewhich resolves once the port has been closed.
Examples
List ports and their information
The following example lists input and output ports, and displays information about them using properties of MIDIPort.
function listInputsAndOutputs( midiAccess ) { for (var entry of midiAccess.inputs) { var input = entry[1]; console.log( "Input port [type:'" + input.type + "'] id:'" + input.id + "' manufacturer:'" + input.manufacturer + "' name:'" + input.name + "' version:'" + input.version + "'" ); } for (var entry of midiAccess.outputs) { var output = entry[1]; console.log( "Output port [type:'" + output.type + "'] id:'" + output.id + "' manufacturer:'" + output.manufacturer + "' name:'" + output.name + "' version:'" + output.version + "'" ); } }
Add available ports to a select list
The following example takes the list of input ports and adds them to a select list, in order that a user can choose the device they want to use.
inputs.forEach( function( port, key ) { var opt = document.createElement("option"); opt.text = port.name; document.getElementById("inputportselector").add(opt); });
Specifications
| Specification |
|---|
| Web MIDI API # MIDIPort |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
MIDIPort |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
close |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
connection |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
id |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
manufacturer |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
name |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
onstatechange |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
open |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
state |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
statechange_event |
43 |
≤79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
type |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
version |
43 |
79 |
No |
No |
30 |
No |
43 |
43 |
No |
30 |
No |
4.0 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MIDIPort