@Override
protected Void doInBackground(Void... arg0) {
isPlaying = true;
int bufferSize = AudioTrack.getMinBufferSize(frequence, channelConfig, audioEncoding);
byte[] buffer = new byte[bufferSize/4];
try {
InputStream is = getResources().openRawResource(R.raw.temp);
//定义输入流,将音频写入到AudioTrack类中,实现播放
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
//实例AudioTrack
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,
frequence,
channelConfig,
audioEncoding,
bufferSize,
AudioTrack.MODE_STREAM);
//开始播放
track.setStereoVolume(1.0f, 1.0f);
track.play();
while(isPlaying && dis.available()>0){
int i = 0;
while(dis.available()>0 && i
buffer[i] = dis.readByte();
i++;
}
//然后将数据写入到AudioTrack中
track.write(buffer, 0, buffer.length);
}
//播放结束
track.stop();
dis.close();
} catch (Exception e) {
// TODO: handle exception
}
return null;
}