android播放本地的pcm
白小花同学 2014-09-30 04:49:02 我想播放科大讯飞的录音生成在本地的PCM,下面是我的方法,但是播放出来的全是噪音。求大神指导!
public void play() {
// Get the file we want to playback./storage/emulated/0/oral/pcm/14091714280386951041300.pcm
File file = new File("/storage/emulated/0/oral/pcm/14091009303394417878374.pcm");
// Get the length of the audio stored in the file (16 bit so 2 bytes per short)
// and create a short array to store the recorded audio.
int musicLength = (int)(file.length()/2);
short[] music = new short[musicLength];
try {
// Create a DataInputStream to read the audio data back from the saved file.
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
// Read the file into the music array.
int i = 0;
while (dis.available() > 0) {
music[i] = dis.readShort();
i++;
}
// Close the input streams.
dis.close();
// Create a new AudioTrack object using the same parameters as the AudioRecord
// object used to create the file.
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
musicLength*2,
AudioTrack.MODE_STREAM);
// Start playback
audioTrack.play();
// Write the music buffer to the AudioTrack object
audioTrack.write(music, 0, musicLength);
audioTrack.stop() ;
} catch (Throwable t) {
Log.e("AudioTrack","Playback Failed");
}
}