Created
December 21, 2015 00:12
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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