Created
March 16, 2012 10:23
-
-
Save hkurosawa/2049459 to your computer and use it in GitHub Desktop.
Playing sound file in assets folder on Android
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
import android.media.MediaPlayer; | |
public class PlayerExample { | |
MediaPlayer p = null; | |
private void playSound(String fileName) { | |
p = new MediaPlayer(); | |
try { | |
AssetFileDescriptor afd = ctx.getAssets().openFd(fileName); | |
p.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); | |
afd.close(); | |
p.prepare(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
p.start(); | |
} | |
} |
How do I make it stop once it has started? @jaredsburrows @hkurosawa
@KidusMT, I believe you call stop()
for MediaPlayer
. See here: https://developer.android.com/reference/android/media/MediaPlayer#stop().
Yes Yes... I did. Thank you! @jaredsburrows
public void stopSound() {
if (mediaPlayer != null)
mediaPlayer.stop();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure you are passing in the
Context
: