我正在为Android 2.1编程。你可以帮助我解决以下问题吗?
我有三个文件,通用目的是通过缓冲区播放录音带缓冲区的声音。我在这里非常绝望,因为我尝试了所有的东西,而且我的扬声器仍然没有声音(而Android的集成媒体播放器通过模拟器播放声音没有问题。)
源代码:
音频层类,用于实现音轨。它将收到一个缓冲区,其中包含声音。
public AudioPlayer(int sampleRate, int channelConfiguration, int audioFormat) throws ProjectException {
minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfiguration, audioFormat);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfiguration,
audioFormat, minBufferSize, AudioTrack.MODE_STREAM);
if(audioTrack == null)
throw new ProjectException("Erreur lors de l'instantiation de AudioTrack");
audioTrack.setStereoVolume((float)1.0, (float)1.0);
}
@Override
public void addToQueue(short[] buffer) {
audioTrack.write(buffer, 0, buffer.length*Short.SIZE);
if(!isPlaying ) {
audioTrack.play();
isPlaying = true;
}
}
模型类,我用它来填充缓冲区。通常情况下,它会从文件中加载声音,但这里只使用模拟器(440Hz)进行调试。
缓冲区大小选择非常松散;通常第一个缓冲区大小应该是6615然后是4410.这也是仅用于调试。
public void onTimeChange() {
if(begin) {
//First fill about 300ms
begin = false;
short[][] buffer = new short[channels][numFramesBegin];
//numFramesBegin is for example 10000
//For debugging only buffer[0] is useful
fillSimulatedBuffer(buffer, framesRead);
framesRead += numFramesBegin;
audioPlayer.addToQueue(buffer[0]);
}
else {
try {
short[][] buffer = new short[channels][numFrames];
//Afterwards fill like 200ms
fillSimulatedBuffer(buffer, framesRead);
framesRead += numFrames;
audioPlayer.addToQueue(buffer[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private short simulator(int time, short amplitude) {
//a pure A (frequency=440)
//this is probably wrong due to sampling rate, but 44 and 4400 won't work either
return (short)(amplitude*((short)(Math.sin((double)(simulatorFrequency*time)))));
}
private void fillSimulatedBuffer(short[][] buffer, int offset) {
for(int i = 0; i < buffer[0].length; i++)
buffer[0][i] = simulator(offset + i, amplitude);
}
每200毫秒调用model.ontimechange()的timeTask类。
public class ReadMusic extends TimerTask {
private final Model model;
public ReadMusic(Model model) {
this.model = model;
}
@Override
public void run() {
System.out.println("Task run");
model.onTimeChange();
}
}
调试显示了我:
任何想法都将不胜感激!
答案 0 :(得分:2)
好的,我发现了问题。
当前AudioTrack文档中有关于AudioTrack和短缓冲区输入的错误:指定的缓冲区大小应该是缓冲区本身的大小(buffer.length),不是大小(以字节为单位)。