android 实时播放,Android AudioTrack实时播放

选择AudioRecord进行录后,实时播放也只能采用AudioTrack进行播放。

public class AudioTrackManager {

public static final String TAG = "AudioTrackManager";

private AudioTrack audioTrack;

private DataInputStream dis;

private Thread recordThread;

private boolean isStart = false;

private static AudioTrackManager mInstance;

private int bufferSize;

public AudioTrackManager() {

bufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize * 2, AudioTrack.MODE_STREAM);

}

/**

* 获取单例引用

*

* @return

*/

public static AudioTrackManager getInstance() {

if (mInstance == null) {

synchronized (AudioTrackManager.class) {

if (mInstance == null) {

mInstance = new AudioTrackManager();

}

}

}

return mInstance;

}

/**

* 销毁线程方法

*/

private void destroyThread() {

try {

isStart = false;

if (null != recordThread && Thread.State.RUNNABLE == recordThread.getState()) {

try {

Thread.sleep(500);

recordThread.interrupt();

} catch (Exception e) {

recordThread = null;

}

}

recordThread = null;

} catch (Exception e) {

e.printStackTrace();

} finally {

recordThread = null;

}

}

/**

* 启动播放线程

*/

private void startThread() {

destroyThread();

isStart = true;

if (recordThread == null) {

recordThread = new Thread(recordRunnable);

recordThread.start();

}

}

/**

* 播放线程

*/

Runnable recordRunnable = new Runnable() {

@Override

public void run() {

try {

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

byte[] tempBuffer = new byte[bufferSize];

int readCount = 0;

while (dis.available() > 0) {

readCount= dis.read(tempBuffer);

if (readCount == AudioTrack.ERROR_INVALID_OPERATION || readCount == AudioTrack.ERROR_BAD_VALUE) {

continue;

}

if (readCount != 0 && readCount != -1) {

audioTrack.play();

audioTrack.write(tempBuffer, 0, readCount);

}

}

stopPlay();

} catch (Exception e) {

e.printStackTrace();

}

}

};

/**

* 播放文件

*

* @param path

* @throws Exception

*/

private void setPath(String path) throws Exception {

File file = new File(path);

dis = new DataInputStream(new FileInputStream(file));

}

/**

* 启动播放

*

* @param path

*/

public void startPlay(String path) {

try {

setPath(path);

startThread();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 停止播放

*/

public void stopPlay() {

try {

destroyThread();

if (audioTrack != null) {

if (audioTrack.getState() == AudioRecord.STATE_INITIALIZED) {

audioTrack.stop();

}

if (audioTrack != null) {

audioTrack.release();

}

}

if (dis != null) {

dis.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

使用前要添加如下权限:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值