Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SampleGuppy extends Activity implements OnClickListener {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_sampleguppy);
- initSound();
- initialiseModel();
- //Set up our buttons
- recordButton = (Button) findViewById(R.id.record_button);
- recordButton.setOnClickListener(this);
- playButton = (Button) findViewById(R.id.playback_button);
- playButton.setOnClickListener(this);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_sampleguppy, menu);
- return true;
- }
- /**
- * Custom controls
- */
- private Button recordButton;
- private Button playButton;
- /**
- * Custom behavior
- */
- private void initialiseModel() {
- // CaptureSingleton.INSTANCE.production = new Production();
- File f = getFilesDir();
- String path = f.getAbsolutePath();
- File newFile = new File(path + "/sampleguppy.settings");
- try {
- newFile.createNewFile();
- }
- catch(IOException e){
- Log.e("Create File", e.getLocalizedMessage());
- }
- }
- /**
- * UI Actions
- */
- boolean isRecording = false;
- //Make a recording and save it to a file
- private void record() {
- if(!isRecording){
- PdBase.sendSymbol("recordfilename", "x.aiff");
- PdBase.sendBang("openfile");
- PdBase.sendBang("record");
- }
- else {
- PdBase.sendBang("stoprecording");
- }
- isRecording = !isRecording;
- }
- //play back the last recording from the file called 'recorded'
- public void play() {
- PdBase.sendSymbol("playfilename", "x.aiff");
- PdBase.sendBang("readfile");
- PdBase.sendBang("play");
- }
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.record_button:
- record();
- break;
- case R.id.playback_button:
- play();
- }
- }
- /**
- * Lib PD
- */
- private PdUiDispatcher dispatcher;
- private void initPd() throws IOException {
- //configure audio glue
- int sampleRate = AudioParameters.suggestSampleRate();
- PdAudio.initAudio(sampleRate, 0, 2, 8, true);
- //setup the dispatcher
- dispatcher = new PdUiDispatcher();
- PdBase.setReceiver(dispatcher);
- }
- private void loadPatch() throws IOException {
- File dir = getFilesDir();
- File patchFile = new File(dir, "recorder5.pd");
- IoUtils.extractZipResource(getResources().openRawResource(R.raw.recorder), dir, true);
- PdBase.openPatch(patchFile.getAbsolutePath());
- }
- private void initSound(){
- try {
- initPd();
- loadPatch();
- }
- catch(IOException e){
- Log.e("Guppyizer", e.toString());
- finish();
- }
- }
- /* Lifecycle methods PD needs */
- @Override
- protected void onStart() {
- super.onStart();
- PdAudio.startAudio(this);
- }
- @Override
- protected void onStop() {
- PdAudio.stopAudio();
- super.onStop();
- }
- @Override
- protected void onResume() {
- super.onResume();
- PdAudio.startAudio(this);
- }
- @Override
- protected void onPause(){
- super.onPause();
- PdAudio.stopAudio();
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- PdAudio.release();
- PdBase.release();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement