Android examples for android.media:Audio
create Media Player to playback the specified audio stream
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import android.media.MediaPlayer; public class Main { private final static String TEMP_PLAYBACKFILE_PATH_PREFIX = "playertemp"; /**/*from w ww . ja v a 2s .c o m*/ * create MediaPlayer object to playback the specified audio stream * * @param stream * specified audio stream * @return MediaPlayer */ public static MediaPlayer createMediaPlayer(InputStream stream) { MediaPlayer mediaplayer = null; try { File temp = File.createTempFile(TEMP_PLAYBACKFILE_PATH_PREFIX, "temp"); String tempPath = temp.getAbsolutePath(); FileOutputStream out = new FileOutputStream(temp); BufferedOutputStream bis = null; try { bis = new BufferedOutputStream(out); byte buf[] = new byte[128]; do { int numread = stream.read(buf); if (numread <= 0) break; bis.write(buf, 0, numread); } while (true); } finally { if (bis != null) { bis.close(); } } MediaPlayer mp = new MediaPlayer(); mp.setDataSource(tempPath); mp.prepare(); mediaplayer = mp; } catch (Exception e) { e.printStackTrace(); } return mediaplayer; } }