我希望有人能指出我正确的方向。我已经能够使录音效果良好,以及电平表。我已经开始播放音频,这让我有些头疼。我已经阅读了有关MediaPlayer和AudioTrack的各种文章以及这两个类之间的一些差异。我读过的一件事是两个类都支持PCM,这是我录制的音频格式。
在音频录制中,我使用AudioRecord类来捕获16位PCM单声道数据。我正在捕获数据,然后将数据写入保存到SD卡的文件中。以下是写入文件的代码的核心:
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;
WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
longSampleRate, channels, byteRate);
while (dis.available() > 0 )
{
dos.writeShort(dis.readShort());
}
以下是编写WAVE标题的代码:
byte[] header = new byte[44];
header[0] = 'R'; // RIFF/WAVE header
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f'; // 'fmt ' chunk
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 16; // 4 bytes: size of 'fmt ' chunk
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1; // format = 1
header[21] = 0;
header[22] = (byte) channels;
header[23] = 0;
header[24] = (byte) (longSampleRate & 0xff);
header[25] = (byte) ((longSampleRate >> 8) & 0xff);
header[26] = (byte) ((longSampleRate >> 16) & 0xff);
header[27] = (byte) ((longSampleRate >> 24) & 0xff);
header[28] = (byte) (byteRate & 0xff);
header[29] = (byte) ((byteRate >> 8) & 0xff);
header[30] = (byte) ((byteRate >> 16) & 0xff);
header[31] = (byte) ((byteRate >> 24) & 0xff);
header[32] = (byte) (2 * 16 / 8); // block align
header[33] = 0;
header[34] = RECORDER_BPP; // bits per sample
header[35] = 0;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte) (totalAudioLen & 0xff);
header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
out.write(header, 0, 44);
问题在于播放音频文件。 MediaPlayer在播放方面非常快,但结果是静态的。我正在使用上面的代码创建的文件,并试图通过MediaPlayer推送它进行播放。根据我的阅读,MediaPlayer支持16位PCM音频,我相信我已在上面创建。
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
File file = new File(mFileName);
FileInputStream fis = new FileInputStream(file);
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
当我使用AudioTrack时,音频播放正常,这意味着音频与录制时一样。使用此类的一个大问题是我在尝试开始播放时遇到的延迟。我正在使用Stream模式,音频开始播放需要5-15秒,这是完全不可接受的。
以下是我调用AudioTrack类的代码:
int intSize = android.media.AudioTrack.getMinBufferSize(RECORDER_SAMPLERATE,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioPlayer = new AudioTrack(AudioManager.STREAM_MUSIC,RECORDER_SAMPLERATE,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
intSize,
AudioTrack.MODE_STREAM);
short[] byteData = null;
File file = null;
file = new File(mFileName);
InputStream is = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try
{
is = new FileInputStream( file );
bis = new BufferedInputStream(is);
dis = new DataInputStream(bis);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
int bytesread = 0, ret = 0;
int size = (int) file.length();
audioPlayer.play();
byteData = new short[size/2];
while (dis.available() > 0 )
{
byteData[ret] = dis.readShort();
ret++;
}
if (byteData != null )
{
// Write the byte array to the track
audioPlayer.write(byteData,0, ret);
bytesread += ret;
}
dis.close();
is.close();
audioPlayer.stop();
audioPlayer.release();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
使用MediaPlayer时可能导致静电的任何想法?或者有关如何在使用AudioTrack时提高性能的想法?