android 判断是否连接钢琴和检测钢琴是哪个按键

67 篇文章 4 订阅
31 篇文章 0 订阅
public class TaskMiActivity extends AppCompatActivity {


    // private MidiPianoLayout midiPianoLayout;


    TextView tv_tv1, tv_tv2;
    Button btn_connect;

    

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        

        setContentView(R.layout.activity_task_mi);

        tv_tv1 = findViewById(R.id.tv_tv1);
        tv_tv2 = findViewById(R.id.tv_tv2);
        fl_key = findViewById(R.id.fl_key);
        btn_connect = findViewById(R.id.btn_connect);

  btn_connect.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               openMidi();
           }
       });


    }




    void openMidi() {
        MidiManager m = (MidiManager) getSystemService(Context.MIDI_SERVICE);
        MidiDeviceInfo[] infos = m.getDevices();
        if (infos != null && infos.length > 0) {
            int num = 0;
            for (int i = 0; i < infos.length; i++) {
                if (infos[i].getOutputPortCount() != 0) {
                    num = i;
                }
            }
            m.openDevice(infos[num], new MidiManager.OnDeviceOpenedListener() {
                @Override
                public void onDeviceOpened(MidiDevice device) {
                    if (device == null) {
                        
                        Toast.makeText(getBaseContext(), "连接设备失败", Toast.LENGTH_SHORT).show();
                    } else {
                        MidiDeviceInfo.PortInfo[] ports = device.getInfo().getPorts();
                        for (int j = 0; j < ports.length; j++) {
                            if (ports[j].getType() == MidiDeviceInfo.PortInfo.TYPE_OUTPUT) {
                                int outputNum = ports[j].getPortNumber();
                                MidiOutputPort midiOutputPort = device.openOutputPort(outputNum);
                                if (midiOutputPort != null) {
                                    
                                    midiOutputPort.connect(new MyReceiver());
                                    
                                    Toast.makeText(getBaseContext(), "连接设备成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    
                                    Toast.makeText(getBaseContext(), "连接设备失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }
            }, new Handler(Looper.getMainLooper()));
        }
    }


    class MyReceiver extends MidiReceiver {
       
        public void onSend(final byte[] data, final int offset,
                           final int count, final long timestamp) throws IOException {
            
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    if (count == 3) {
                        int jw = data[2];

                     
                        tv_tv1.setText("键位:" + jw + "-------" + data[3] + " -----" + offset + "  -----" + count + " ---------" + timestamp);

                        Log.e("ContentValues", "键位:" + jw);
                        Log.e("ContentValues", "力道:" + data[3]);
                        Log.e("ContentValues", "count:" + count);
                        Log.e("ContentValues", "offset:" + offset);
                        Log.e("ContentValues", "timestamp:" + timestamp);
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < data.length; i++) {
                           
                            sb.append("key:" + i + "<-->");
                            sb.append("value:" + data[i]);
                            
                        }
                      

                        tv_tv2.setText("-----" + "所有数据:" + sb.toString());
                      
                    }
                }
            });
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值