Android MIDI Threading InteruptedException – сообщения Aftertouch

Попытка запуска MIDI в моем приложении для Android. Я следую примеру midisuite для настройки моего приложения, и он отлично работает, за исключением aftertouch. Всякий раз, когда я пытаюсь вызвать aftertouch, я сталкиваюсь с типом исключения InteruptedException . Как предотвратить эту проблему с потоками? Мои знания о многопоточности не самые лучшие, иначе я бы уже понял это. Все, что я могу сказать прямо сейчас, это то, что сообщение отправляется слишком быстро, и поток еще не проснулся от вызова сна.

Я выполнил gitub-репо с моим кодом следующим образом:

Подкласс MidiReceiver :

 @TargetApi(Build.VERSION_CODES.M) public class MidiEngine extends MidiReceiver { public AudioActivity activity; private MidiEventScheduler eventScheduler; private MidiFramer midiFramer; private MidiReceiver midiReceiver = new MyReceiver(); private Thread mThread; private boolean go; private int mProgram; public MidiEngine() { this(new AudioActivity()); } public MidiEngine(AudioActivity activity) { this.activity = activity; midiReceiver = new MyReceiver(); midiFramer = new MidiFramer(midiReceiver); } public AudioActivity getActivity() { return this.activity; } /* This will be called when MIDI data arrives. */ @Override public void onSend(byte[] data, int offset, int count, long timestamp) throws IOException { if (eventScheduler != null) { if (!MidiConstants.isAllActiveSensing(data, offset, count)) { eventScheduler.getReceiver().send(data, offset, count, timestamp); } } } // Custom Listener to send to correct methods private class MyReceiver extends MidiReceiver { @Override public void onSend(byte[] msg, int offset, int count, long timestamp) throws IOException { byte command = (byte)(msg[0] & MidiConstants.STATUS_COMMAND_MASK); int channel = (byte)(msg[0] & MidiConstants.STATUS_CHANNEL_MASK); switch (command) { case MidiConstants.STATUS_NOTE_ON: activity.keyDown(i, msg[1], msg[2]); break; case MidiConstants.STATUS_NOTE_OFF: activity.keyUp(channel, msg[1]); break; case MidiConstants.STATUS_POLYPHONIC_AFTERTOUCH: activity.keyDown(channel, msg[1], msg[2]); break; case MidiConstants.STATUS_PITCH_BEND: activity.pitchBendAction(channel, (msg[2] << 7) + msg[1]); break; case MidiConstants.STATUS_CONTROL_CHANGE: activity.ccAction(channel, msg[1], msg[2]); break; case MidiConstants.STATUS_PROGRAM_CHANGE: mProgram = msg[1]; break; default: break; } } } class MyRunnable implements Runnable { @Override public void run() { do { try { activity.runOnUiThread(new Runnable() { @Override public void run() { try { processMidiEvents(); } catch (Exception e) { Log.e("Java", "SynthEngine background thread exception.", e); } } }); Thread.sleep(100); } catch (InterruptedException e) { Log.e("Java", "Threading exception", e); } } while (go); } } /** * @throws IOException * */ private void processMidiEvents() throws IOException { long now = System.nanoTime(); MidiEventScheduler.MidiEvent event = (MidiEventScheduler.MidiEvent) eventScheduler.getNextEvent(now); while (event != null) { midiFramer.send(event.data, 0, event.count, event.getTimestamp()); eventScheduler.addEventToPool(event); event = (MidiEventScheduler.MidiEvent) eventScheduler.getNextEvent(now); } } public void start() { stop(); go = true; mThread = new Thread(new MyRunnable()); mThread.setPriority(6); eventScheduler = new MidiEventScheduler(); mThread.start(); } public void stop() { go = false; if (mThread != null) { try { mThread.interrupt(); mThread.join(500); } catch (Exception e) { } mThread = null; eventScheduler = null; } } } 

Ошибка трассировки стека (строка 154 относится к части Thread.sleep в моем классе Runnable ):

 Java: Threading exception java.lang.InterruptedException at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:1031) at java.lang.Thread.sleep(Thread.java:985) at com.rfoo.midiapp.communication.MidiEngineInput$MyRunnable.run(MidiEngineInput.java:154) at java.lang.Thread.run(Thread.java:818) 

Благодаря!

EDIT: начало темы

Подкласс Midi Device Service (поток запускается всякий раз, когда устройство подключено или отключено).

 @TargetApi(Build.VERSION_CODES.M) public class MidiSynthDeviceService extends MidiDeviceService { private static final String TAG = "MidiSynthDeviceService"; private boolean midiStarted = false; @Override public void onCreate() { super.onCreate(); } @Override public void onDestroy() { AudioActivity.midiEngine.stop(); super.onDestroy(); } @Override // Declare the receivers associated with your input ports. public MidiReceiver[] onGetInputPortReceivers() { return new MidiReceiver[] { AudioActivity.midiEngine }; } /** * This will get called when clients connect or disconnect. * You can use it to turn on your synth only when needed. */ @Override public void onDeviceStatusChanged(MidiDeviceStatus status) { if (status.isInputPortOpen(0) && !midiStarted) { AudioActivity.midiEngine.start(); midiStarted = true; } else if (!status.isInputPortOpen(0) && midiStarted){ AudioActivity.midiEngine.stop(); midiStarted = false; } } } 

Класс деятельности:

 public class AudioActivity extends AppCompatActivity { private Thread thread; public static MidiEngine midiEngine; @Override protected void onCreate(Bundle savedInstanceState) { // Layout inits super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); // Setup MIDI: if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI)) { Toast.makeText(this, "MIDI not supported!", Toast.LENGTH_LONG).show(); } else { midiEngine = new MidiEngine(this); setupMidi(); } // Setup audio thread: if (thread == null) { thread = new Thread() { public void run() { setPriority(Thread.MAX_PRIORITY); // Runs an Open SL audio thread (C++) // This generates a waveform. // AudioEngine is a wrapper class connecting C++ to Java AudioEngine.runProcess(); } } } } public void setupMidi() { if (activity == null) activity = (AudioActivity) getContext(); mMidiManager = (MidiManager) activity.getSystemService(AudioActivity.MIDI_SERVICE); if (mMidiManager == null) { Toast.makeText(activity, "MidiManager is null!", Toast.LENGTH_LONG).show(); return; } // Get Device Info MidiDeviceInfo deviceInfo = MidiTools.findDevice(mMidiManager, "RFOO", "AudioApp"); // MIDI Input portIndex = 0; inputPortSelector = new MidiOutputPortConnectionSelector(mMidiManager, activity, R.id .inputListView, deviceInfo, portIndex); inputPortSelector.setConnectedListener(new MyPortsConnectedListener()); midi_ch_input = 0; midi_ch_output = 0; } // Bunch of UI code here.... } 

Solutions Collecting From Web of "Android MIDI Threading InteruptedException – сообщения Aftertouch"