作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我如何从 Pd 接收数据到我的 Android 应用程序?我设法向 Pd 发送了一个 float ,并在 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 作为 float 。难道是补丁无法访问麦克风?我的 Android list 使用权限录制音频。
最佳答案
//pdAudio.initAudio(sampleRate,inputchannels,outputchannels,ticksperbuffer,restart?)
PdAudio.initAudio(sampleRate,1,2,8,true);
/* setting Input channels to 2 on a device that has only 1 mic throws Exceptions
* Code should be improved take advantage of all input channels on different devices
*/
dispatcher = new PdUiDispatcher();
PdBase.setReceiver(dispatcher);
//dispatcher.addListener("senderObjectFromPD",Listener); in my case "highLow"
dispatcher.addListener("highLow", myListener);
关于java - 如何从 libpd pd-for-android 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34803288/
我是一名优秀的程序员,十分优秀!