android – 通过Eclipse上的缓冲区播放AudioTrack缓冲区音乐 – 没有声音
作者:互联网
我正在为Android 2.1编程.你可以帮助我解决以下问题吗?
我有三个文件,通用的目的是通过缓冲区播放带有audiotrack缓冲区的声音.我在这里非常绝望,因为我尝试了所有的东西,而且我的扬声器仍然没有声音(而android的集成媒体播放器通过模拟器播放声音没有问题).
源代码:
一个audioplayer类,实现音轨.它将收到一个缓冲区,其中包含声音.
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的演奏状态是“正在播放”
>模型函数中没有异常.
任何想法将不胜感激!
解决方法:
好的我发现了问题.
关于AudioTrack和短缓冲区输入的当前AudioTrack文档中存在错误:指定的缓冲区大小应该是缓冲区本身的大小(buffer.length)而不是以字节为单位的大小.
标签:android,timer,buffer,audiotrack 来源: https://codeday.me/bug/20190704/1378237.html