简体   繁体   English

如何在libPd中播放WAV文件?

[英]How to play wav file in libPd?

I have a simple libPd project. 我有一个简单的libPd项目。 Now, I have downloaded a .wav file and I want to play it instead the pure-data file that I have. 现在,我已经下载了一个.wav文件,我想播放它而不是我拥有的纯数据文件。

Is it possible to do that? 有可能这样做吗?

If it does, how can I do that? 如果可以,我该怎么办?

This is my project: 这是我的项目:

public class MainActivity extends AppCompatActivity {

private Button kickButton, bassButton;
private PdUiDispatcher dispatcher;
private boolean isClicked;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    kickButton = (Button) findViewById(R.id.kickButton);
    bassButton = (Button) findViewById(R.id.bassButton);

    int sampleRat = AudioParameters.suggestSampleRate();
    try {
        PdAudio.initAudio(sampleRat,0,2,8,true);
        dispatcher = new PdUiDispatcher();
        PdBase.setReceiver(dispatcher);

        File dir = getFilesDir();
        IoUtils.extractZipResource(getResources().openRawResource(R.raw.simplepatch),dir,true);
        File pdPatch = new File(dir, "simplepatch.pd");
        PdBase.openPatch(pdPatch.getAbsolutePath());

    } catch (IOException e) {
        e.printStackTrace();
    }



    kickButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isClicked = !isClicked;

            float val = (isClicked) ? 1.0f : 0.0f;
            PdBase.sendFloat("onOff", val);
        }
    });

}

@Override
protected void onResume() {
    super.onResume();
    PdAudio.startAudio(this);
}

@Override
protected void onPause() {
    super.onPause();
    PdAudio.stopAudio();
}
}

To load and play a wav file, you need to zip your pd patch and the wav file into one zip file. 要加载和播放wav文件,您需要将pd补丁和wav文件压缩为一个zip文件。 In your java code, your zip file is called simplepatch.zip and then you open the patch simplepatch.pd from inside the zip file. 在您的Java代码中,您的zip文件称为simplepatch.zip,然后从zip文件内部打开补丁simplepatch.pd。

File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.simplepatch),dir,true);
File pdPatch = new File(dir, "simplepatch.pd");
PdBase.openPatch(pdPatch.getAbsolutePath());

All you have to do is put the wav file into the simplepach.zip file and then using a button in your android app, play the file. 您要做的就是将wav文件放入simplepach.zip文件,然后使用android应用中的按钮播放文件。 A sample pd patch for it would look like this: 一个示例pd补丁看起来像这样:

click to view image 点击查看图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM