Skip to content

Instantly share code, notes, and snippets.

@timpulver
Created December 21, 2015 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timpulver/ad697bb49862961b5366 to your computer and use it in GitHub Desktop.
Save timpulver/ad697bb49862961b5366 to your computer and use it in GitHub Desktop.
Plays an audio / sound file (Mp3) on Android using Processing and the MediaPlayer class.
/**
* Android Audio Player example, copied from https://forum.processing.org/two/discussion/12819/how-do-i-get-sound-to-play-on-the-phone
*
* Some audio formats seem not to be recognized (maybe some MP3-codecs as well), put the audio file into the data-directory.
*/
import android.media.MediaPlayer;
import android.content.res.AssetFileDescriptor;
import android.content.Context;
import android.app.Activity;
MediaPlayer mp;
Context context;
Activity act;
AssetFileDescriptor afd;
void setup() {
act = this.getActivity();
context = act.getApplicationContext();
try {
mp = new MediaPlayer();
afd = context.getAssets().openFd("279.mp3");//which is in the data folder
println("Successfully loaded audio file");
mp.setDataSource(afd.getFileDescriptor());
mp.prepare();
}
catch(IOException e) {
println("file did not load");
}
mp.start();
};
void draw() {
}
public void pause() {
super.pause();
if (mp !=null) {
mp.release();
mp = null;
}
};
public void stop() {
super.stop();
if (mp !=null) {
mp.release();
mp = null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment