如何从 Pd 接收数据到我的 Android 应用程序?我设法向 Pd 发送了一个浮点数,并在 Pd 上模拟了一个接收对象,只是为了查看我期望的结果。这是我到目前为止所拥有的。
public class MainActivity extends AppCompatActivity {
private PdUiDispatcher dispatcher;
Button newActivity;
Switch highLow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGUI();
try{
initPD();
loadPD_Patch();
loadPD_RecordPatch();
}catch (IOException error){
Log.e("Pd initialization error", String.valueOf(error));
}
}
private void initGUI(){
Switch onOffSwitch = (Switch) findViewById(R.id.onOffSwitch);
highLow = (Switch)findViewById(R.id.highLow);
newActivity = (Button) findViewById(R.id.newActivity);
onOffSwitch.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
float val = (isChecked) ? 1.0f : 0.0F;
PdBase.sendFloat("onOff", val);
}
}
);
}
private void initPD() throws IOException{
int sampleRate = AudioParameters.suggestSampleRate();
PdAudio.initAudio(sampleRate,0,2,8,true);
dispatcher = new PdUiDispatcher();
PdBase.setReceiver(dispatcher);
dispatcher.addListener("highLow", myListener);
}
private void loadPD_Patch() throws IOException{
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.simple_android_patch), dir, true);
File pdPatch = new File(dir,"simple_android_patch.pd");
PdBase.openPatch(pdPatch.getAbsoluteFile());
}
private void loadPD_RecordPatch() throws IOException{
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.audio_in_android), dir, true);
File pdPatch = new File(dir,"audio_in_android.pd");
PdBase.openPatch(pdPatch.getAbsoluteFile());
}
private final PdListener myListener = new PdListener() {
@Override
public void receiveMessage(String source, String symbol, Object... args) {
Log.i("receiveMessage symbol:", symbol);
for (Object arg: args) {
Log.i("receiveMessage atom:", arg.toString());
}
}
/* Receive a list from Pd */
@Override
public void receiveList(String source, Object... args) {
for (Object arg: args) {
Log.i("receiveList atom:", arg.toString());
}
}
/* Receive symbol from Pd */
@Override public void receiveSymbol(String source, String symbol) {
Log.i("receiveSymbol", symbol);
}
/* Receive float from Pd */
@Override public void receiveFloat(String source, float x) {
Log.e("highLow", String.valueOf(x));
if(x == 1.0){
highLow.setChecked(true);
}else {
highLow.setChecked(false);
}
}
/* Recieve bang from Pd */
@Override public void receiveBang(String source) {
Log.i("receiveBang", "bang!");
}
};
@Override
protected void onResume(){
super.onResume();
PdAudio.startAudio(this);
}
@Override
protected void onPause(){
super.onPause();
PdAudio.stopAudio();
}
}
这是我的 Pd 补丁,
即使我鼓掌或发出噪音,终端也会继续显示它从 Pd 补丁的“highLow”发送对象接收 0.0 作为浮点数。会不会是补丁无法访问麦克风?我的 Android 清单使用权限录制音频。