我正在为 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();
}
}
调试向我展示了什么:
- timeTask 工作正常,它完成了它的工作;
- 缓冲区值看起来是连贯的,并且缓冲区大小大于 minBufSize;
- Audiotrack 的播放状态为“正在播放”
- 模型函数中没有异常。
任何想法将不胜感激!