如何使用 libpd 在 Android 中播放波形文件?我可以用 来做到这一点soundpool.play
,但我想试试 libpd。我按照本教程实现了 libpd,但它不起作用。有什么问题?代码还是 PD 补丁?
这是我的活动代码:
public class MainActivity extends ActionBarActivity implements OnTouchListener {
private PdUiDispatcher dispatcher;
private void initPD() throws IOException {
int sampleRate = AudioParameters.suggestSampleRate();
PdAudio.initAudio(sampleRate, 0, 2, 8, true);
dispatcher = new PdUiDispatcher();
PdBase.setReceiver(dispatcher);
}
private void loadPDPatch() throws IOException {
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.playaudio), dir, true);
File pdPatch = new File(dir, "playaudio.pd");
PdBase.openPatch(pdPatch.getAbsolutePath());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bangButton = (Button) findViewById(R.id.bangButton);
bangButton.setOnTouchListener(this);
try {
initPD();
loadPDPatch();
PdAudio.startAudio(this);
} catch (IOException e) {
finish();
}
}
@Override
protected void onResume() {
super.onResume();
PdAudio.startAudio(this);
}
@Override
protected void onPause() {
super.onPause();
PdAudio.stopAudio();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
if(v.getId() == R.id.bangButton) {
PdBase.sendBang("mybang");
}
return false;
}
}
这是我的 pd 补丁:
#N canvas 0 22 902 577 24;
#X obj 46 24 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1 1
;
#X obj 47 248 dac~;
#X obj 48 193 readsf~;
#X obj 49 118 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
-1;
#X msg 42 61 \; pd dsp \$1;
#X msg 50 155 open myfile.wav \, 1;
#X connect 0 0 4 0;
#X connect 2 0 1 0;
#X connect 3 0 5 0;
#X connect 5 0 2 0;