public class PlayAudioManager {
private static MediaPlayer mediaPlayer;
public static void playAudio(final Context context, final String url) throws Exception {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(context, Uri.parse(url));
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
killMediaPlayer();
}
});
mediaPlayer.start();
}
private static void killMediaPlayer() {
if (mediaPlayer != null) {
try {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
In your Activity,example,just call
PlayAudioManager.playAudio(context, http://dict.youdao.com/dictvoice?audio=good&type=1)