I am trying to follow the institutions within the android.media.midi and the MidiDevice classes. Nothing works.
I now believe that the implementation of these classes is beyond the scope of my comprehension.
//these methods are called by and action bar item
//open the listener device. Device Info was passed as the selected device
openDevice(deviceInfo, null, null);
//open the output port 2
openOutputPort(2);
//open the input port 1
openInputPort(1);
}
// From class MidiDevice open the device
public void openDevice(MidiDeviceInfo[] device, OnDeviceOpenedListener listener, Handler handler)
{
this.listener = listener;
TextView textout = null;
//send the information to a textview
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
//the only thing that comes through is device = void upon the initial call. The listener initiates no action.
textout.setText(String.format("Midi Keydown detected Device = %s", device + " handler = " +handler));
}
//Class Myreciever was extracted from MidiScope. It was also tried. It appears to do nothing.
private class MyReceiver extends MidiReceiver {
@Override
public void onSend(byte[] data, int offset, int count,
long timestamp) throws IOException {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Midi Keydown detected Handler");
}
}
// here is where the port is opened my devices only need output port 2
public boolean openOutputPort(int i) {
return true;
}
// the input port is opened. They are never used.
public void openInputPort(int i){
}
}
//
//open the listener device. Device Info was passed as the selected device
openDevice(deviceInfo, null, null);
//Class Myreciever was extracted from MidiScope. It was also tried. It appears to do nothing.
private class MyReceiver extends MidiReceiver {
Thanks for your help Phil Burk. I tried your code and it did not work. I put the supplied code into a simplest Hello world example (below). Several error segments of the code light up in red. The errors are displayed in the comments and within the attached image. There seems no way to fix this. Perhaps there is a global error in my coding.
import android.graphics.Color;
import android.media.midi.MidiDevice;
import android.media.midi.MidiDeviceInfo;
import android.media.midi.MidiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Frank Znidarsic code
final MidiDeviceInfo info;
MidiManager m = (MidiManager) getSystemService(MIDI_SERVICE);
final MidiDeviceInfo[] deviceInfos = m.getDevices();
//code supplied by Phill burk
info = deviceInfos[0];
//error cannot resolve symbol MidiManager
midiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
TextView textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Device did not open! " + info);
} else {
TextView textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Got open device! " + info);
//error cannot resolve symbol mdevice
mDevice = device;
//error unexpected token after comma and Looper ())
},new Handler(Looper.getMainLooper())
//error Unexpected token after ")"
);
}}
//error expected )

m.openDevice(info, new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
Log.e(TAG, "could not open device " + info);
} else {
// ...
}
}
}, new Handler(Looper.getMainLooper()));
--
You received this message because you are subscribed to the Google Groups "android-midi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-midi...@googlegroups.com.
To post to this group, send email to androi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-midi/3b9a326f-1494-4d5f-b94b-a7b317ba2fe7%40googlegroups.com.
MidiDevice device =null;
public void open_device() {
//Call Midimanager onCreate method
MidiManager m = (MidiManager) getSystemService(MIDI_SERVICE);
//init data types
// MidiDevice device = null;
MidiDeviceInfo[] info = m.getDevices();
final MidiDeviceInfo info0 = info[0];
//get property name
Bundle properties = info0.getProperties();
final String manufacturer = properties.getString(MidiDeviceInfo.PROPERTY_NAME);
//Open an instance of the class openDevice
m.openDevice(info0, new OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Device failed to open");
} else {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Device opened = " + manufacturer);
//open oputput port #2 on MIDI device
//put here to obtain device info and to avoid null pointer exception error
int index = 2;
MidiOutputPort outputPort = device.openOutputPort(index);
outputPort.connect(new MyReceiver());
}
int index = 2;
}
}, new Handler(Looper.getMainLooper()));
}
//Class Myreciever
class MyReceiver extends MidiReceiver {
The method m.openDevice(info0, new OnDeviceOpenedListener() apparently takes device info and returns device. That is what Android Studio asks for.
When it runs, the Android Marshmallow device returns. "Unfortunately Your Program Has Stopped." This error goes away if outputPort.connect(new MyReceiver()); is commented out. I want to open output port 2 on the MIDI that is why I set index to 2.
public void open_device() {
//Call Midimanager onCreate method
MidiManager m = (MidiManager) getSystemService(MIDI_SERVICE);
//init data types
// MidiDevice device = null;
MidiDeviceInfo[] info = m.getDevices();
final MidiDeviceInfo info0 = info[0];
//get property name
Bundle properties = info0.getProperties();
final String manufacturer = properties.getString(MidiDeviceInfo.PROPERTY_NAME);
//Open an instance of the class openDevice
m.openDevice(info0, new OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
if (device == null) {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Device failed to open");
} else {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Device opened = " + manufacturer
);
//open output port #0 on MIDI device
int index = 0;
MidiOutputPort outputPort = device.openOutputPort(index);
outputPort.connect(
new MyReceiver());
}
}
}, new Handler(Looper.getMainLooper()));
}
//Class Myreciever
class MyReceiver extends MidiReceiver {
public void onSend(byte[] data, int offset,
int count, long timestamp) throws
IOException {
if (data[2] > 80) {
TextView textout = null;
textout=(TextView)
findViewById(R.id.log);
textout.setText("keydown = "+data[2]);
}
}
}
}
//method open_device opens midi device Called by menu item or a button
public void open_device() {
//Call Midimanager onCreate method
MidiManager m = (MidiManager) getSystemService(MIDI_SERVICE);
//init data types
MidiDeviceInfo[] info = m.getDevices();
//use device zero there may be others but for this simple example zero works
final MidiDeviceInfo info0 = info[0];
//use the port count in a test to determine if the device cans open
final int numOutputs = info0.getOutputPortCount();
//get the name of the MIDI keyboard this will be used to show which device has opened
Bundle properties = info0.getProperties();
final String manufacturer = properties.getString(MidiDeviceInfo.PROPERTY_NAME);
//Open an instance of the class openDevice
m.openDevice(info0, new OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
//port number is < 1 if device is connected
if ( numOutputs < 1) {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Device failed to open. Try disconnecting/reconnecting OTG cable. ");
} else {
// send out device has oepned message
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.BLUE);
textout.setText("Device opened = " + manufacturer);
//open output device #0 on MIDI device, there my be others but for this example zero is start
int index = 0;
MidiOutputPort outputPort = device.openOutputPort(index);
outputPort.connect(new MyReceiver());
}
}
}, new Handler(Looper.getMainLooper()));
}
//Class Myreceiver receives MIDI data from a que.
class MyReceiver extends MidiReceiver {
public void onSend(byte[] data, int offset, int count, long timestamp) throws IOException {
//detect mid c, note number 60, or higher
if (data[2] > 59) {
// prints nicely to logcat
System.out.println("Keydown = "+ data[2]);
// error Android with text view “Only the original thread that created a view hierarchy can touch its views.”
// TextView textout9 = null;
// textout9 = (TextView) findViewById(R.id.log);textout9.setTextColor(Color.BLUE);
// textout9.setText("keydown = " + data[2]);
}
}
}
}
class MyReceiver extends MidiReceiver {
public void onSend(byte[] data, int offset, int count, long timestamp) throws IOException {
//detect mid c, note number 60, or higher
if (data[2] > 59) {
// tried to send the action out to a method show_c that contains image views. Does not work.
// generates, Only the original thread that created the the hierarchy can touch the text views.
show_c();
// prints nicely to logcat commented out for this test.
// System.out.println("Keydown = "+ data[2]);
Hi,
You probably need to use Activity.runOnUiThread:
There is a similar example to your use case called MidiScope:
@Override
public void log(final String string) {
runOnUiThread(new Runnable() {
@Override
public void run() {
logOnUiThread(string);
}
});
}
/**
* Logs a message to our TextView. This needs to be called from the UI thread.
*/
private void logOnUiThread(String s) {
mLogLines.add(s);
if (mLogLines.size() > MAX_LINES) {
mLogLines.removeFirst();
}
// Render line buffer to one String.
StringBuilder sb = new StringBuilder();
for (String line : mLogLines) {
sb.append(line).append('\n');
}
mLog.setText(sb.toString());
mScroller.fullScroll(View.FOCUS_DOWN);
}
Regards,
Pedro
--
You received this message because you are subscribed to the Google Groups "android-midi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-midi...@googlegroups.com.
To post to this group, send email to androi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-midi/150e30a7901-d8e-cc1d%40webstg-m04.mail.aol.com.
class MyReceiver extends MidiReceiver {
public void onSend(byte[] data, int offset, int count, long timestamp) throws IOException {
//detect mid c, note number 60, or higher
if (data[2] > 59) {
// called an instance of PrintMidi to get around the error cannot rewrite to a textview
PrintMidi m = new PrintMidi();
m.PrintOut();
}
Its no wonder Java is frustrating. I did make a lot of progress except for this one last error. A very basic example of the a Midi receiver was implemented. The new example is about 50 steps, not 800. It's all in the main and easy to follow. Its a starting place to build from. It is different from the supplied examples as listed below:
These differences include.
Using if( numOutputs < 1) instead of null to test for if a device is open.
Re phrasing the looper. Thanks Pedro.
Moving int index = 0;
MidiOutputPort outputPort = device.openOutputPort(index);
outputPort.connect(new MyReceiver());
into the m.ondevce opened method. The supplied example shows these two lines of code at the bottom of the onSend method.Knowing to set the index to zero not to two for the first simple example.Setting the first parameter in m.openDevice(info0
by calling, final MidiDeviceInfo info0 = info[0];
Knowing to call m.openDevice from a button or menu but not to call the onDevice opened class.
Thank you Phil for getting me there. The basics of a MidiReceiver are now done.
Frank Znidarsic
//method open_device opens midi device Called by menu item or a buttonThis opens output port 2 on the MIDI and device 0;
public void open_device() {
//Call Midimanager onCreate method
MidiManager m = (MidiManager) getSystemService(MIDI_SERVICE);
//init data types
MidiDeviceInfo[] info = m.getDevices();
//use device zero there may be others but for this simple example zero works
final MidiDeviceInfo info0 = info[0
];
//use the port count in a test to determine if the device cans open
final int numOutputs = info0.getOutputPortCount();
//get the name of the MIDI keyboard this will be used to show which device has opened
Bundle properties = info0.getProperties();
final String manufacturer = properties.getString(MidiDeviceInfo.PROPERTY_NAME
);
//Open an instance of the class openDevice
m.openDevice(info0, new OnDeviceOpenedListener() {
int singleshot = 0;
@Override
public void onDeviceOpened(MidiDevice device) {
//port number is < 1 if device is connected
if (numOutputs < 1) {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Device failed to open. Try disconnecting/reconnecting OTG cable. ");
} else {
// send out device has opened message
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.BLUE);
textout.setText("Device opened = " + manufacturer);
//open output device #0 on MIDI device, there my be others but for this example zero is start
int index = 0;
MidiOutputPort outputPort = device.openOutputPort(index);
outputPort.connect(new
MyReceiver());
}
}
}, new Handler(Looper.getMainLooper()));
}
//Class Myreceiver receives MIDI data from a que.
class MyReceiver extends MidiReceiver {
public void onSend(final byte[] data, int offset, int count, long timestamp) throws IOException {
//detect mid c, note number 60, or higher
if (data[2] > 59) {
// error Android with text view “Only the original thread that created a view hierarchy can touch its views.” is fixed with the runonUiThread command.
runOnUiThread(new Runnable() {
@Override
public void
run() {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.BLUE);
textout.setText("keydown = " + data[2] );
}
});
}
}
}
}
The solution using the new MIDI methods and a logger was like pulling teeth (for me). My code is pasted below. It requires an associated XML file with a text view named log. The code is much easier to understand than the complex MidiScope. It contains no context, wrappers or, loggers, It can be used as an aid in the basic understanding MIDI scope. I'm on my way to getting my first app going. From there I am going to work with a hardware developer to produce hard wired MIDI interfaces and apps. I can do it!
//method open_device opens midi device Called by menu item or a buttonThis opens output port 2 on the MIDI and device 0;
public void open_device() {
//Call Midimanager
MidiManager m = (MidiManager) getSystemService(MIDI_SERVICE);
//init data types
MidiDeviceInfo[] info = m.getDevices();
//use device zero there may be others but for this simple example zero works
final MidiDeviceInfo info0 = info[0
];
//use the port count in a test to determine if the device cans open
final int numOutputs = info0.getOutputPortCount();
//get the name of the MIDI keyboard this will be used to show which device has opened
Bundle properties = info0.getProperties();
final String manufacturer = properties.getString(MidiDeviceInfo.PROPERTY_NAME);
//Open an instance of the class openDevice
m.openDevice(info0, new OnDeviceOpenedListener() {
int singleshot = 0;
@Override
public void
onDeviceOpened(MidiDevice device) {
//If port number is < 1 the device is connected
if (numOutputs < 1) {
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.RED);
textout.setText("Device failed to open. Try disconnecting/reconnecting OTG cable. ");
} else {
// send out device has opened message
TextView textout = null;
textout = (TextView) findViewById(R.id.log);
textout.setTextColor(Color.BLUE);
textout.setText("Device opened = " + manufacturer);
//open output device #0 on MIDI device, there my be others but for this example zero is start
int index = 0;
MidiOutputPort outputPort = device.openOutputPort(index);
outputPort.connect(new
MyReceiver());
}
}
}, new Handler(Looper.getMainLooper()));
}
//Class Myreceiver receives MIDI data from a que.
class MyReceiver extends MidiReceiver {
public void onSend(final byte[] data, int offset, int count, long timestamp) throws IOException {
//detect mid c, note number 60, or higher is pressedif (data[2] > 59) {
mdevice.closeOutputPort(index);
//reset screen saver and close object device upon exit from the app
public void onDestroy() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (mdevice != null){
try {
mdevice.close();
} catch (IOException e) {
e.printStackTrace();
}
super.onDestroy();
}
}
I am looking for the method to close the MIDI port. I tried the code below. It does not work.mdevice.closeOutputPort(index);
I will look for the close port method again. Does anyone see it?
I am looking for the method to close the MIDI port. I tried the code below. It does not work.mdevice.closeOutputPort(index);
Casio Prima does not send keyup only keydown. This was fixed by using only keyup and or'ed with key impact ==0.