0% found this document useful (0 votes)
201 views

Apwidget Android Audio

This document provides code for playing audio files on an Android device using Processing. It creates an APMediaPlayer object to handle audio playback. The code sets the audio file, starts playback, sets looping and volume, and displays playback details. Key presses allow controlling playback through actions like seeking, playing, pausing, and changing files. The APMediaPlayer is released on app destroy to avoid crashes.

Uploaded by

adulphusjuhnos
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views

Apwidget Android Audio

This document provides code for playing audio files on an Android device using Processing. It creates an APMediaPlayer object to handle audio playback. The code sets the audio file, starts playback, sets looping and volume, and displays playback details. Key presses allow controlling playback through actions like seeking, playing, pausing, and changing files. The APMediaPlayer is released on app destroy to avoid crashes.

Uploaded by

adulphusjuhnos
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// This sketch has been tested in Processing 2.0a4 // Android SDK was installed as described at // http://wiki.processing.

org/w/Android // In that page, note the part that says Processing 2.0 is required // Remember to use "Run on Device" // (CTRL + SHIFT + R or SHIFT + click on play) import apwidgets.*; APMediaPlayer player; void setup() { player = new APMediaPlayer(this); } void draw() { } void mousePressed() { if(mouseY > height / 2) { player.setMediaFile("test2.mp3"); } else { player.setMediaFile("test.mp3"); } player.start(); } public void onDestroy() { super.onDestroy(); if(player != null) { player.release(); } } import apwidgets.*; APMediaPlayer player; void setup() { player = new APMediaPlayer(this); //create new APMediaPlayer player.setMediaFile("test.mp3"); //set the file (files are in data folder) player.start(); //start play back player.setLooping(true); //restart playback end reached player.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0 } void draw() { background(0); //set black background text(player.getDuration(), 10, 10); //display the duration of the sound text(player.getCurrentPosition(), 10, 30); //display how far the sound has pla yed }

void keyPressed() { if (key == CODED) { if (keyCode == LEFT) { player.seekTo(0); //"rewind" } else if (keyCode == RIGHT) { player.start(); //start playing sound file } else if (keyCode == DPAD) { player.pause(); //pause player } else if (keyCode == DOWN) { player.setMediaFile("test2.mp3"); //switch to other sound file } } } //The MediaPlayer must be released when the app closes public void onDestroy() { super.onDestroy(); //call onDestroy on super class if(player!=null) { //must be checked because or else crash when return from la ndscape mode player.release(); //release the player import apwidgets.*; APMediaPlayer player; void setup() { player = new APMediaPlayer(this); //create new APMediaPlayer player.setMediaFile("test.mp3"); //set the file (files are in data folder) player.start(); //start play back player.setLooping(true); //restart playback end reached player.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0 } void draw() { background(0); //set black background text(player.getDuration(), 10, 10); //display the duration of the sound text(player.getCurrentPosition(), 10, 30); //display how far the sound has pla yed } void mousePressed() { player.start(); //start playing sound file } //The MediaPlayer must be released when the app closes public void onDestroy() { super.onDestroy(); //call onDestroy on super class

if(player!=null) { //must be checked because or else crash when return from la ndscape mode player.release(); //release the player } } REMINDER: Try putting your mp3 resource in a folder named "data" ... When exporting your a pplet, Processing will move files in this "data" folder in the "assets" folder, for correct loading on your Android device. } }

You might also like