Java tutorial
//package com.java2s; import android.media.AudioManager; import android.media.MediaPlayer; import java.io.IOException; public class Main { private static MediaPlayer sMediaPlayer; public static void playSound(String filePath) { playSound(filePath, null); } public static void playSound(String filePath, MediaPlayer.OnCompletionListener listener) { if (sMediaPlayer == null) { sMediaPlayer = new MediaPlayer(); sMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mediaPlayer, int what, int extra) { sMediaPlayer.reset(); return false; } }); } else { sMediaPlayer.reset(); } try { sMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); if (listener != null) { sMediaPlayer.setOnCompletionListener(listener); } sMediaPlayer.setDataSource(filePath); sMediaPlayer.prepare(); sMediaPlayer.start(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }