public class AudioThread implements Runnable {
private final int AUDIO_BUF_SIZE = 160;
private final int FRAME_INFO_SIZE = 16;
@Override
public void run() {
System.out.printf(“[%s] Start\n”, Thread.currentThread().getName());
AVAPIs av = new AVAPIs();
byte[] frameInfo = new byte[FRAME_INFO_SIZE];
byte[] audioBuffer = new byte[AUDIO_BUF_SIZE];
int[] frameNumber = new int[1];
byte[] pcm = new byte[320];
audioCodec mAudioCodec = new audioCodec();
int size = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
// MODE_IN_COMMUNICATION
AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, // 指定在流的类型
8000, AudioFormat.CHANNEL_OUT_MONO,// 设置输出声道为双声道立体声
AudioFormat.ENCODING_PCM_16BIT,// 设置音频数据块是8位还是16位
size*2 , AudioTrack.MODE_STREAM);
mAudioTrack.setStereoVolume(1.0f, 1.0f);// 设置当前音量大
mAudioTrack.play();
int ret;
int _playPositon = 0;
while (isAudioRecv.get()) {
ret = av.avCheckAudioBuf(avcodec.this.avindex.get());
if (ret < 0) {
// Same error codes as below
System.out.printf(“[%s] avCheckAudioBuf() failed: %d\n”,
Thread.currentThread().getName(), ret);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
} else if (ret < 3) {
try {
Thread.sleep(200);
continue;
} catch (InterruptedException e) {
System.out.println(e.getMessage());
continue;
}
}
mAudioTrack.stop();
mAudioTrack.release();
mAudioCodec = null;
}
}
private final int AUDIO_BUF_SIZE = 160;
private final int FRAME_INFO_SIZE = 16;
@Override
public void run() {
System.out.printf(“[%s] Start\n”, Thread.currentThread().getName());
AVAPIs av = new AVAPIs();
byte[] frameInfo = new byte[FRAME_INFO_SIZE];
byte[] audioBuffer = new byte[AUDIO_BUF_SIZE];
int[] frameNumber = new int[1];
byte[] pcm = new byte[320];
audioCodec mAudioCodec = new audioCodec();
int size = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
// MODE_IN_COMMUNICATION
AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, // 指定在流的类型
8000, AudioFormat.CHANNEL_OUT_MONO,// 设置输出声道为双声道立体声
AudioFormat.ENCODING_PCM_16BIT,// 设置音频数据块是8位还是16位
size*2 , AudioTrack.MODE_STREAM);
mAudioTrack.setStereoVolume(1.0f, 1.0f);// 设置当前音量大
mAudioTrack.play();
int ret;
int _playPositon = 0;
while (isAudioRecv.get()) {
ret = av.avCheckAudioBuf(avcodec.this.avindex.get());
if (ret < 0) {
// Same error codes as below
System.out.printf(“[%s] avCheckAudioBuf() failed: %d\n”,
Thread.currentThread().getName(), ret);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
} else if (ret < 3) {
try {
Thread.sleep(200);
continue;
} catch (InterruptedException e) {
System.out.println(e.getMessage());
continue;
}
}
mAudioTrack.stop();
mAudioTrack.release();
mAudioCodec = null;
}
}
解决方案
20
传入到audiotrack的pcm数据不连续,好长时间没有传入数据,就会导致audiotrack里面的track 被remove掉,再次传入数据的时候,又会重新创建track,你这个log就是重新创建track的时候打印出来的。不知你之前是不是有很多underrun这样的log?