下面列出了怎么用android.media.midi.MidiManager的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mInputPort = mOpenDevice.openInputPort(
wrapper.getPortIndex());
if (mInputPort == null) {
Log.e(MidiConstants.TAG, "could not open input port on " + info);
}
}
}
}, null);
// Don't run the callback on the UI thread because openInputPort might take a while.
}
}
/**
* @return a device that matches the manufacturer and product or null
*/
public static MidiDeviceInfo findDevice(MidiManager midiManager,
String manufacturer, String product) {
for (MidiDeviceInfo info : midiManager.getDevices()) {
String deviceManufacturer = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_MANUFACTURER);
if ((manufacturer != null)
&& manufacturer.equals(deviceManufacturer)) {
String deviceProduct = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_PRODUCT);
if ((product != null) && product.equals(deviceProduct)) {
return info;
}
}
}
return null;
}
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mInputPort = mOpenDevice.openInputPort(
wrapper.getPortIndex());
if (mInputPort == null) {
Log.e(MidiConstants.TAG, "could not open input port on " + info);
}
}
}
}, null);
// Don't run the callback on the UI thread because openInputPort might take a while.
}
}
/**
* @return a device that matches the manufacturer and product or null
*/
public static MidiDeviceInfo findDevice(MidiManager midiManager,
String manufacturer, String product) {
for (MidiDeviceInfo info : midiManager.getDevices()) {
String deviceManufacturer = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_MANUFACTURER);
if ((manufacturer != null)
&& manufacturer.equals(deviceManufacturer)) {
String deviceProduct = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_PRODUCT);
if ((product != null) && product.equals(deviceProduct)) {
return info;
}
}
}
return null;
}
private void findMidiDevice() {
MidiDeviceInfo[] infos = midiManager.getDevices();
for(MidiDeviceInfo info : infos) {
String name = info.getProperties().getString(MidiDeviceInfo.PROPERTY_NAME);
logger.log("Found MIDI device named " + name);
if(TEENSY_MIDI_NAME.equals(name)) {
logger.log("^^^ using this device ^^^");
isConnecting = true;
midiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
logger.log("Error, unable to open MIDI device");
} else {
logger.log("Opened MIDI device successfully!");
midiDevice = device;
}
isConnecting = false;
}
}, null);
break;
}
}
}
private void openInCurrentThread() {
TGActivity activity = TGActivityController.getInstance(this.context).getActivity();
MidiManager midiManager = (MidiManager) activity.getSystemService(Context.MIDI_SERVICE);
midiManager.openDevice(this.info, new MidiManager.OnDeviceOpenedListener() {
public void onDeviceOpened(MidiDevice device) {
try {
if (device != null) {
openInputPort(device);
}
} finally {
updateConnectingStatus(false);
}
}
}, new Handler(Looper.getMainLooper()));
}
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mInputPort = mOpenDevice.openInputPort(
wrapper.getPortIndex());
if (mInputPort == null) {
Log.e(MidiConstants.TAG, "could not open input port on " + info);
}
}
}
}, null);
// Don't run the callback on the UI thread because openInputPort might take a while.
}
}
/**
* @return a device that matches the manufacturer and product or null
*/
public static MidiDeviceInfo findDevice(MidiManager midiManager,
String manufacturer, String product) {
for (MidiDeviceInfo info : midiManager.getDevices()) {
String deviceManufacturer = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_MANUFACTURER);
if ((manufacturer != null)
&& manufacturer.equals(deviceManufacturer)) {
String deviceProduct = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_PRODUCT);
if ((product != null) && product.equals(deviceProduct)) {
return info;
}
}
}
return null;
}
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mInputPort = mOpenDevice.openInputPort(
wrapper.getPortIndex());
if (mInputPort == null) {
Log.e(MidiConstants.TAG, "could not open input port on " + info);
}
}
}
}, null);
// Don't run the callback on the UI thread because openInputPort might take a while.
}
}
/**
* @return a device that matches the manufacturer and product or null
*/
public static MidiDeviceInfo findDevice(MidiManager midiManager,
String manufacturer, String product) {
for (MidiDeviceInfo info : midiManager.getDevices()) {
String deviceManufacturer = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_MANUFACTURER);
if ((manufacturer != null)
&& manufacturer.equals(deviceManufacturer)) {
String deviceProduct = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_PRODUCT);
if ((product != null) && product.equals(deviceProduct)) {
return info;
}
}
}
return null;
}
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mInputPort = mOpenDevice.openInputPort(
wrapper.getPortIndex());
if (mInputPort == null) {
Log.e(MidiConstants.TAG, "could not open input port on " + info);
}
}
}
}, null);
// Don't run the callback on the UI thread because openInputPort might take a while.
}
}
/**
* @return a device that matches the manufacturer and product or null
*/
public static MidiDeviceInfo findDevice(MidiManager midiManager,
String manufacturer, String product) {
for (MidiDeviceInfo info : midiManager.getDevices()) {
String deviceManufacturer = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_MANUFACTURER);
if ((manufacturer != null)
&& manufacturer.equals(deviceManufacturer)) {
String deviceProduct = info.getProperties()
.getString(MidiDeviceInfo.PROPERTY_PRODUCT);
if ((product != null) && product.equals(deviceProduct)) {
return info;
}
}
}
return null;
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUEST_BLUETOOTH_SCAN && resultCode == RESULT_OK) {
final BluetoothDevice fBluetoothDevice = (BluetoothDevice) data
.getParcelableExtra("device");
if (fBluetoothDevice != null) {
Log.i(TAG, "Bluetooth device name = "
+ fBluetoothDevice.getName()
+ ", address = "
+ fBluetoothDevice.getAddress());
mMidiManager.openBluetoothDevice(fBluetoothDevice,
new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
onBluetoothDeviceOpen(fBluetoothDevice, device);
}
}, null);
}
}
}
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
Log.i(MidiConstants.TAG, "onPortSelected: " + wrapper);
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mOutputPort = device.openOutputPort(wrapper.getPortIndex());
if (mOutputPort == null) {
Log.e(MidiConstants.TAG,
"could not open output port for " + info);
return;
}
mOutputPort.connect(mDispatcher);
}
}
}, null);
// Don't run the callback on the UI thread because openOutputPort might take a while.
}
}
/**
* @param midiManager
* @param activity
* @param spinnerId
* @param type
*/
public MidiOutputPortConnectionSelector(MidiManager midiManager,
Activity activity, int spinnerId,
MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex) {
super(midiManager, activity, spinnerId,
MidiDeviceInfo.PortInfo.TYPE_OUTPUT);
mDestinationDeviceInfo = destinationDeviceInfo;
mDestinationPortIndex = destinationPortIndex;
}
/**
* Open a source device and connect its output port to the
* destinationInputPort.
*
* @param sourceDeviceInfo
* @param sourcePortIndex
* @param destinationInputPort
*/
private void connectToDevicePort(final MidiDeviceInfo sourceDeviceInfo,
final int sourcePortIndex,
final MidiInputPort destinationInputPort,
final OnPortsConnectedListener listener, final Handler handler) {
mMidiManager.openDevice(sourceDeviceInfo,
new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG,
"could not open " + sourceDeviceInfo);
safeClose();
if (listener != null) {
listener.onPortsConnected(null);
}
} else {
Log.i(MidiConstants.TAG,
"connectToDevicePort opened "
+ sourceDeviceInfo);
// Device was opened so connect the ports.
mSourceDevice = device;
mConnection = device.connectPorts(
destinationInputPort, sourcePortIndex);
if (mConnection == null) {
Log.e(MidiConstants.TAG, "could not connect to "
+ sourceDeviceInfo);
safeClose();
}
if (listener != null) {
listener.onPortsConnected(mConnection);
}
}
}
}, handler);
}
private void setupMidi() {
// Setup MIDI
MidiManager midiManager = (MidiManager) getSystemService(MIDI_SERVICE);
MidiDeviceInfo synthInfo = MidiTools.findDevice(midiManager, "AndroidTest",
"SynthExample");
int portIndex = 0;
mPortSelector = new MidiOutputPortConnectionSelector(midiManager, this,
R.id.spinner_synth_sender, synthInfo, portIndex);
mPortSelector.setConnectedListener(new MyPortsConnectedListener());
}
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
Log.i(MidiConstants.TAG, "onPortSelected: " + wrapper);
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mOutputPort = device.openOutputPort(wrapper.getPortIndex());
if (mOutputPort == null) {
Log.e(MidiConstants.TAG,
"could not open output port for " + info);
return;
}
mOutputPort.connect(mDispatcher);
}
}
}, null);
// Don't run the callback on the UI thread because openOutputPort might take a while.
}
}
/**
* @param midiManager
* @param activity
* @param spinnerId
* @param type
*/
public MidiOutputPortConnectionSelector(MidiManager midiManager,
Activity activity, int spinnerId,
MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex) {
super(midiManager, activity, spinnerId,
MidiDeviceInfo.PortInfo.TYPE_OUTPUT);
mDestinationDeviceInfo = destinationDeviceInfo;
mDestinationPortIndex = destinationPortIndex;
}
/**
* Open a source device and connect its output port to the
* destinationInputPort.
*
* @param sourceDeviceInfo
* @param sourcePortIndex
* @param destinationInputPort
*/
private void connectToDevicePort(final MidiDeviceInfo sourceDeviceInfo,
final int sourcePortIndex,
final MidiInputPort destinationInputPort,
final OnPortsConnectedListener listener, final Handler handler) {
mMidiManager.openDevice(sourceDeviceInfo,
new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG,
"could not open " + sourceDeviceInfo);
safeClose();
if (listener != null) {
listener.onPortsConnected(null);
}
} else {
Log.i(MidiConstants.TAG,
"connectToDevicePort opened "
+ sourceDeviceInfo);
// Device was opened so connect the ports.
mSourceDevice = device;
mConnection = device.connectPorts(
destinationInputPort, sourcePortIndex);
if (mConnection == null) {
Log.e(MidiConstants.TAG, "could not connect to "
+ sourceDeviceInfo);
safeClose();
}
if (listener != null) {
listener.onPortsConnected(mConnection);
}
}
}
}, handler);
}
MidiTest(Context context) {
super(context);
inputRepetitions = getIntPreference(context, R.string.preference_midi_in_reps, 100);
outputRepetitions = getIntPreference(context, R.string.preference_midi_out_reps, 10);
midiManager = (MidiManager) context.getSystemService(Context.MIDI_SERVICE);
findMidiDevice();
}
public List<MidiOutputPort> listPorts() {
if( this.ports == null ) {
this.ports = new ArrayList<MidiOutputPort>();
} else {
this.ports.clear();
}
if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M ) {
TGActivity activity = TGActivityController.getInstance(this.context).getActivity();
if( activity != null && activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI)) {
MidiManager midiManager = (MidiManager) activity.getSystemService(Context.MIDI_SERVICE);
MidiDeviceInfo[] infos = midiManager.getDevices();
for(MidiDeviceInfo info : infos) {
if( info.getInputPortCount() > 0 ) {
MidiDeviceInfo.PortInfo[] portInfos = info.getPorts();
for(MidiDeviceInfo.PortInfo portInfo : portInfos) {
if( portInfo.getType() == MidiDeviceInfo.PortInfo.TYPE_INPUT) {
this.ports.add(new MidiOutputPortImpl(this.context, info, portInfo));
}
}
}
}
}
}
return this.ports;
}
private void openDevice(final MidiDeviceInfo info) {
mManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
MidiManagerAndroid.this.onDeviceOpened(device, info);
}
}, mHandler);
}
private void setupMidi() {
// Setup MIDI
MidiManager midiManager = (MidiManager) getSystemService(MIDI_SERVICE);
MidiDeviceInfo synthInfo = MidiTools.findDevice(midiManager, "AndroidTest",
"SynthExample");
int portIndex = 0;
mPortSelector = new MidiOutputPortConnectionSelector(midiManager, this,
R.id.spinner_synth_sender, synthInfo, portIndex);
mPortSelector.setConnectedListener(new MyPortsConnectedListener());
}
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
Log.i(MidiConstants.TAG, "onPortSelected: " + wrapper);
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mOutputPort = device.openOutputPort(wrapper.getPortIndex());
if (mOutputPort == null) {
Log.e(MidiConstants.TAG,
"could not open output port for " + info);
return;
}
mOutputPort.connect(mDispatcher);
}
}
}, null);
// Don't run the callback on the UI thread because openOutputPort might take a while.
}
}
/**
* @param midiManager
* @param activity
* @param spinnerId
* @param type
*/
public MidiOutputPortConnectionSelector(MidiManager midiManager,
Activity activity, int spinnerId,
MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex) {
super(midiManager, activity, spinnerId,
MidiDeviceInfo.PortInfo.TYPE_OUTPUT);
mDestinationDeviceInfo = destinationDeviceInfo;
mDestinationPortIndex = destinationPortIndex;
}
/**
* Open a source device and connect its output port to the
* destinationInputPort.
*
* @param sourceDeviceInfo
* @param sourcePortIndex
* @param destinationInputPort
*/
private void connectToDevicePort(final MidiDeviceInfo sourceDeviceInfo,
final int sourcePortIndex,
final MidiInputPort destinationInputPort,
final OnPortsConnectedListener listener, final Handler handler) {
mMidiManager.openDevice(sourceDeviceInfo,
new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG,
"could not open " + sourceDeviceInfo);
safeClose();
if (listener != null) {
listener.onPortsConnected(null);
}
} else {
Log.i(MidiConstants.TAG,
"connectToDevicePort opened "
+ sourceDeviceInfo);
// Device was opened so connect the ports.
mSourceDevice = device;
mConnection = device.connectPorts(
destinationInputPort, sourcePortIndex);
if (mConnection == null) {
Log.e(MidiConstants.TAG, "could not connect to "
+ sourceDeviceInfo);
safeClose();
}
if (listener != null) {
listener.onPortsConnected(mConnection);
}
}
}
}, handler);
}
@Override
public void onPortSelected(final MidiPortWrapper wrapper) {
Log.i(MidiConstants.TAG, "onPortSelected: " + wrapper);
close();
final MidiDeviceInfo info = wrapper.getDeviceInfo();
if (info != null) {
mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG, "could not open " + info);
} else {
mOpenDevice = device;
mOutputPort = device.openOutputPort(wrapper.getPortIndex());
if (mOutputPort == null) {
Log.e(MidiConstants.TAG,
"could not open output port for " + info);
return;
}
mOutputPort.connect(mDispatcher);
}
}
}, null);
// Don't run the callback on the UI thread because openOutputPort might take a while.
}
}
/**
* @param midiManager
* @param activity
* @param spinnerId
* @param type
*/
public MidiOutputPortConnectionSelector(MidiManager midiManager,
Activity activity, int spinnerId,
MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex) {
super(midiManager, activity, spinnerId,
MidiDeviceInfo.PortInfo.TYPE_OUTPUT);
mDestinationDeviceInfo = destinationDeviceInfo;
mDestinationPortIndex = destinationPortIndex;
}
/**
* Open a source device and connect its output port to the
* destinationInputPort.
*
* @param sourceDeviceInfo
* @param sourcePortIndex
* @param destinationInputPort
*/
private void connectToDevicePort(final MidiDeviceInfo sourceDeviceInfo,
final int sourcePortIndex,
final MidiInputPort destinationInputPort,
final OnPortsConnectedListener listener, final Handler handler) {
mMidiManager.openDevice(sourceDeviceInfo,
new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(MidiConstants.TAG,
"could not open " + sourceDeviceInfo);
safeClose();
if (listener != null) {
listener.onPortsConnected(null);
}
} else {
Log.i(MidiConstants.TAG,
"connectToDevicePort opened "
+ sourceDeviceInfo);
// Device was opened so connect the ports.
mSourceDevice = device;
mConnection = device.connectPorts(
destinationInputPort, sourcePortIndex);
if (mConnection == null) {
Log.e(MidiConstants.TAG, "could not connect to "
+ sourceDeviceInfo);
safeClose();
}
if (listener != null) {
listener.onPortsConnected(mConnection);
}
}
}
}, handler);
}